Skip to content

Commit

Permalink
Merge 936c7e9 into ee24cc0
Browse files Browse the repository at this point in the history
  • Loading branch information
zhquan committed Feb 10, 2023
2 parents ee24cc0 + 936c7e9 commit dce1a33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
10 changes: 10 additions & 0 deletions releases/unreleased/sirmordred-logs-available-on-console.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: SirMordred logs available on console
category: added
author: Quan Zhou <quan@bitergia.com>
issue: null
notes: >
All SirMordred logs are now available on the console output.
The `logs_dir` parameter in the `general` section is optional
and it is only needed if you also want the logs in a file.
28 changes: 16 additions & 12 deletions sirmordred/bin/sirmordred.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2019 Bitergia
# Copyright (C) 2015-2023 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -60,7 +60,7 @@ def main():
try:
config = Config(args.config_files[0], args.config_files[1:])
config_dict = config.get_conf()
logs_dir = config_dict['general']['logs_dir']
logs_dir = config_dict['general'].get('logs_dir', None)
debug_mode = config_dict['general']['debug']
logger = setup_logs(logs_dir, debug_mode)
except RuntimeError as error:
Expand All @@ -87,20 +87,24 @@ def setup_logs(logs_dir, debug_mode):

logger = logging.getLogger()
logger.setLevel(logging_mode)
# create file handler which logs even debug messages

fh_filepath = os.path.join(logs_dir, 'all.log')
fh = logging.FileHandler(fh_filepath)
fh.setLevel(logging_mode)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)

# create file handler
if logs_dir:
fh_filepath = os.path.join(logs_dir, 'all.log')
fh = logging.FileHandler(fh_filepath)
fh.setLevel(logging_mode)
# Set format
fh.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)

# create console handler
ch = logging.StreamHandler()
ch.setLevel(logging_mode)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

# this is needed because the perceval log messages are similar to ELK/mordred ones
Expand Down
4 changes: 2 additions & 2 deletions sirmordred/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def general_params(cls):
"description": "Debug mode (logging mainly)"
},
"logs_dir": {
"optional": False,
"default": "logs",
"optional": True,
"default": None,
"type": str,
"description": "Directory with the logs of sirmordred"
},
Expand Down

0 comments on commit dce1a33

Please sign in to comment.