Skip to content

Commit

Permalink
Added test for WMCore.WMLogging.MyTimedRotatingFileHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
d-ylee committed Apr 10, 2024
1 parent 5db9166 commit 6228aa0
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions test/python/WMCore_t/WMLogging_t.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
#!/usr/bin/env python
# encoding: utf-8
"""
Test case for WMLogging module
"""
from builtins import range
from datetime import date
import logging
import unittest
import os
from WMCore.WMLogging import CouchHandler

from WMCore.WMLogging import CouchHandler, MyTimedRotatingFileHandler
from WMCore.Database.CMSCouch import CouchServer


class WMLoggingTest(unittest.TestCase):
"""
Unit tests for WMLogging module.
"""
def setUp(self):
# Make an instance of the server
self.server = CouchServer(os.getenv("COUCHURL", 'http://admin:password@localhost:5984'))
testname = self.id().split('.')[-1]
# Create a database, drop an existing one first
self.dbname = 'cmscouch_unittest_%s' % testname.lower()
self.dbname = f'cmscouch_unittest_{testname.lower()}'

if self.dbname in self.server.listDatabases():
self.server.deleteDatabase(self.dbname)
Expand All @@ -25,23 +34,37 @@ def tearDown(self):
# This used to test self._exc_info to only run on success. Broke in 2.7. Removed.
self.server.deleteDatabase(self.dbname)

def testRotatingLogHandler(self):
"""
Test to make sure a date is in or will be added to the logName
"""
todayStr = date.today().strftime("%Y%m%d")
logName = "mylog.log"
handler = MyTimedRotatingFileHandler(logName, 'midnight', backupCount=10)
self.assertIn(todayStr, handler.baseFilename)

logName = f"mylog-{todayStr}.log"
handler = MyTimedRotatingFileHandler(logName, 'midnight', backupCount=10)
self.assertIn(todayStr, handler.baseFilename)

def testLog(self):
"""
Write ten log messages to the database at three different levels
"""
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
myLogger = logging.getLogger('MyLogger')
myLogger.setLevel(logging.DEBUG)
handler = CouchHandler(self.server.url, self.dbname)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
my_logger.addHandler(handler)
myLogger.addHandler(handler)

for _ in range(10):
my_logger.debug('This is probably all noise.')
my_logger.info('Jackdaws love my big sphinx of quartz.')
my_logger.error('HOLLY CRAP!')
myLogger.debug('This is probably all noise.')
myLogger.info('Jackdaws love my big sphinx of quartz.')
myLogger.error('HOLLY CRAP!')
logs = self.db.allDocs()['rows']
self.assertEqual(30, len(logs))


if __name__ == "__main__":
unittest.main()

0 comments on commit 6228aa0

Please sign in to comment.