Skip to content

Commit

Permalink
Added date to filename for MyTimedRotatingFileHandler if it doesn't have
Browse files Browse the repository at this point in the history
it
  • Loading branch information
d-ylee committed Apr 9, 2024
1 parent dd78352 commit 67865e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/python/WMCore/REST/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
from cherrypy.lib import profiler

### Tools is needed for CRABServer startup: it sets up the tools attributes
import WMCore.REST.Tools
from WMCore.Configuration import ConfigSection, loadConfigurationFile
from WMCore.WMLogging import getTimeRotatingLogger, MyTimedRotatingFileHandler
from WMCore.WMLogging import getTimeRotatingLogger
from Utils.Utilities import lowerCmsHeaders
from Utils.PythonVersion import PY2

Expand Down
20 changes: 17 additions & 3 deletions src/python/WMCore/WMLogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import codecs
from datetime import date, timedelta
from logging.handlers import HTTPHandler, RotatingFileHandler, TimedRotatingFileHandler
from pathlib import Path

# a new log level which is lower than debug
# to prevent a tsunami of log messages in debug
Expand All @@ -23,6 +24,7 @@ def sqldebug(msg):
"""
logging.log(logging.SQLDEBUG, msg)


def setupRotatingHandler(fileName, maxBytes = 200000000, backupCount = 3):
"""
_setupRotatingHandler_
Expand All @@ -35,7 +37,8 @@ def setupRotatingHandler(fileName, maxBytes = 200000000, backupCount = 3):


def getTimeRotatingLogger(name, logFile, duration = 'midnight'):
""" Set the logger for time based lotaing.
"""
Set the logger for time based rotating.
"""
logger = logging.getLogger(name)
if duration == 'midnight':
Expand All @@ -62,7 +65,18 @@ class MyTimedRotatingFileHandler(TimedRotatingFileHandler):
https://stackoverflow.com/questions/338450/timedrotatingfilehandler-changing-file-name
"""
def __init__(self, logName, interval, backupCount):
super(MyTimedRotatingFileHandler, self).__init__(logName, when=interval,
"""
Initializes MyTimedRotatingFileHandler
If logName doesn't contain a date, the day of runtime will be added
"""
logPath = Path(logName)
todayStr = date.today().strftime("%Y%m%d")

if todayStr not in logName:
logPath = logPath.parent.joinpath(f"{logPath.stem}-{todayStr}{logPath.suffix}")

super(MyTimedRotatingFileHandler, self).__init__(logPath, when=interval,
backupCount=backupCount)

def doRollover(self):
Expand All @@ -71,7 +85,7 @@ def doRollover(self):
Rotate the log file and add the date between the log name
and its extension, e.g.:
reqmgr2.log becomes reqmgr2-20170815.log
reqmgr2-20170814.log becomes reqmgr2-20170815.log
"""
self.stream.close()
# replace yesterday's date by today
Expand Down

0 comments on commit 67865e1

Please sign in to comment.