Skip to content

Commit

Permalink
feat(audit): logging improvements
Browse files Browse the repository at this point in the history
Refs #1041
  • Loading branch information
chrisjrd committed Dec 13, 2024
1 parent 0456ea8 commit d3f5ee3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
27 changes: 25 additions & 2 deletions tools/ops/cmr_audit/cmr_audit_hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import functools
import logging
import logging.handlers
import os
import re
import sys
Expand All @@ -20,7 +21,7 @@
# format="%(asctime)s %(levelname)7s %(name)4s:%(filename)8s:%(funcName)22s:%(lineno)3s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG)
logger = logging.getLogger()
logger = logging.getLogger(__name__)

config = {
**dotenv_values("../../.env"),
Expand All @@ -42,17 +43,35 @@ def create_parser():
)
argparser.add_argument(
"--output", "-o",
help=f'ISO formatted datetime string. Must be compatible with CMR.'
help=f'Output filepath.'
)
argparser.add_argument(
"--format",
default="txt",
choices=["txt", "json"],
help=f'Output file format. Defaults to "%(default)s".'
)
argparser.add_argument('--log-level', default='INFO', choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'))

return argparser


def init_logging(log_level=logging.INFO):
log_file_format = "%(asctime)s %(levelname)7s %(name)13s:%(filename)19s:%(funcName)22s:%(lineno)3s - %(message)s"
log_format = "%(levelname)s: %(relativeCreated)7d %(process)d %(processName)s %(thread)d %(threadName)s %(name)s:%(filename)s:%(funcName)s:%(lineno)s - %(message)s"
logging.basicConfig(level=log_level, format=log_format, datefmt="%Y-%m-%d %H:%M:%S", force=True)

rfh1 = logging.handlers.RotatingFileHandler('cmr_audit_hls.log', mode='a', maxBytes=100 * 2 ** 20, backupCount=10)
rfh1.setLevel(logging.INFO)
rfh1.setFormatter(logging.Formatter(fmt=log_file_format))
logging.getLogger().addHandler(rfh1)

rfh2 = logging.handlers.RotatingFileHandler('cmr_audit_hls-error.log', mode='a', maxBytes=100 * 2 ** 20, backupCount=10)
rfh2.setLevel(logging.ERROR)
rfh2.setFormatter(logging.Formatter(fmt=log_file_format))
logging.getLogger().addHandler(rfh2)


#######################################################################
# CMR AUDIT FUNCTIONS
#######################################################################
Expand Down Expand Up @@ -259,4 +278,8 @@ async def run(argv: list[str]):


if __name__ == "__main__":
args = create_parser().parse_args(sys.argv[1:])
log_level = args.log_level
init_logging()

asyncio.run(run(sys.argv))
6 changes: 3 additions & 3 deletions tools/ops/cmr_audit/cmr_audit_slc.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ def create_parser():
return argparser


def init_logging():
def init_logging(log_level=logging.INFO):
log_file_format = "%(asctime)s %(levelname)7s %(name)13s:%(filename)19s:%(funcName)22s:%(lineno)3s - %(message)s"
log_format = "%(levelname)s: %(relativeCreated)7d %(process)d %(processName)s %(thread)d %(threadName)s %(name)s:%(filename)s:%(funcName)s:%(lineno)s - %(message)s"
logging.basicConfig(level=log_level, format=log_format, datefmt="%Y-%m-%d %H:%M:%S", force=True)

rfh1 = logging.handlers.RotatingFileHandler('cmr_audit.log', mode='a', maxBytes=100 * 2 ** 20, backupCount=10)
rfh1 = logging.handlers.RotatingFileHandler('cmr_audit_slc.log', mode='a', maxBytes=100 * 2 ** 20, backupCount=10)
rfh1.setLevel(logging.INFO)
rfh1.setFormatter(logging.Formatter(fmt=log_file_format))
logging.getLogger().addHandler(rfh1)

rfh2 = logging.handlers.RotatingFileHandler('cmr_audit-error.log', mode='a', maxBytes=100 * 2 ** 20, backupCount=10)
rfh2 = logging.handlers.RotatingFileHandler('cmr_audit_slc-error.log', mode='a', maxBytes=100 * 2 ** 20, backupCount=10)
rfh2.setLevel(logging.ERROR)
rfh2.setFormatter(logging.Formatter(fmt=log_file_format))
logging.getLogger().addHandler(rfh2)
Expand Down

0 comments on commit d3f5ee3

Please sign in to comment.