Skip to content

Commit

Permalink
Change: Adjust default logging configuration
Browse files Browse the repository at this point in the history
Allow logging to stdout and file at the same time. With this commit the
default logging configuration is:

```
[handlers]
keys=console,file,syslog

[formatters]
keys=file,syslog

[formatter_file]
format=OSPD[$PID] %(asctime)s: %(levelname)s: (%(name)s) %(message)s

[formatter_syslog]
format=OSPD[$PID] %(levelname)s: (%(name)s) %(message)s

[handler_console]
class=logging.StreamHandler
level=INFO
formatter=file
args=sys.stdout

[handler_syslog]
class=handlers.SysLogHandler
level=INFO
formatter=syslog
args=("/dev/log", handlers.SysLogHandler.LOG_USER)

[handler_file]
class=handlers.WatchedFileHandler
level=INFO
formatter=file
args=("/dev/null", "a")

[loggers]
keys=root

[logger_root]
level=NOTSET
handlers=file
propagate=0
```

Depending on the `--foreground` and `--log-file` arguments only the
logger_root handlers value is adjusted. Additionally If `--log-file` is
set the handler_file args is updated to point to the value of the
argument.
  • Loading branch information
bjoernricks committed Feb 6, 2023
1 parent 440f10a commit 52ef7a0
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions ospd/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import configparser
import logging
import os
import configparser
import time

from logging.config import fileConfig
from pathlib import Path
from typing import Optional


DEFAULT_HANDLER_CONSOLE = {
'class': 'logging.StreamHandler',
'level': 'INFO',
Expand All @@ -33,9 +31,10 @@
}

DEFAULT_HANDLER_FILE = {
'class': 'FileHandler',
'class': 'handlers.WatchedFileHandler',
'level': 'INFO',
'formatter': 'file',
'args': '("/dev/null", "a")',
}

DEFAULT_HANDLER_SYSLOG = {
Expand All @@ -45,24 +44,21 @@
'args': '("/dev/log", handlers.SysLogHandler.LOG_USER)',
}

DEFAULT_HANDLERS = {'keys': 'default_handler'}
DEFAULT_HANDLERS = {'keys': 'console,file,syslog'}
DEFAULT_FORMATTERS = {'keys': 'file,syslog'}
DEFAULT_FORMATTER_FILE = {
'format': 'OSPD['
+ str(os.getpid())
+ '] %(asctime)s: %(levelname)s: (%(name)s) %(message)s',
'format': f'OSPD[{os.getpid()}] %(asctime)s: %(levelname)s: '
'(%(name)s) %(message)s',
'datefmt': '',
}
DEFAULT_FORMATTER_SYSLOG = {
'format': 'OSPD['
+ str(os.getpid())
+ '] %(levelname)s: (%(name)s) %(message)s',
'format': f'OSPD[{os.getpid()}] %(levelname)s: (%(name)s) %(message)s',
'datefmt': '',
}
DEFAULT_LOGGERS = {'keys': 'root'}
DEFAULT_ROOT_LOGGER = {
'level': 'NOTSET',
'handlers': 'default_handler',
'handlers': 'file',
'propagate': '0',
}

Expand All @@ -73,28 +69,40 @@ def init_logging(
log_file: Optional[str] = None,
log_config: Optional[str] = None,
foreground: Optional[bool] = False,
):
) -> None:
config = configparser.ConfigParser()
config['handlers'] = DEFAULT_HANDLERS
config['formatters'] = DEFAULT_FORMATTERS
config['formatter_file'] = DEFAULT_FORMATTER_FILE
config['formatter_syslog'] = DEFAULT_FORMATTER_SYSLOG
config['handler_console'] = DEFAULT_HANDLER_CONSOLE
config['handler_syslog'] = DEFAULT_HANDLER_SYSLOG
config['handler_file'] = DEFAULT_HANDLER_FILE
config['loggers'] = DEFAULT_LOGGERS
config['logger_root'] = DEFAULT_ROOT_LOGGER

if foreground:
config['handler_default_handler'] = DEFAULT_HANDLER_CONSOLE
elif log_file:
config['handler_default_handler'] = DEFAULT_HANDLER_FILE
config['handler_default_handler']['args'] = "('" + log_file + "', 'a')"
else:
config['handler_default_handler'] = DEFAULT_HANDLER_SYSLOG
config['logger_root']['handlers'] = 'console'

if log_file:
if foreground:
config['logger_root']['handlers'] = 'console,file'
else:
config['logger_root']['handlers'] = 'file'

config['handler_file']['args'] = f"('{log_file}', 'a')"

if not foreground and not log_file:
config['logger_root']['handlers'] = 'syslog'

config['handler_file']['level'] = log_level
config['handler_console']['level'] = log_level
config['handler_syslog']['level'] = log_level

config['handler_default_handler']['level'] = log_level
log_config_path = Path(log_config)

if log_config_path.exists():
config.read(log_config)
else:
config['loggers'] = DEFAULT_LOGGERS
config['logger_root'] = DEFAULT_ROOT_LOGGER

fileConfig(config, disable_existing_loggers=False)
logging.Formatter.converter = time.gmtime
Expand Down

0 comments on commit 52ef7a0

Please sign in to comment.