Skip to content

Commit ab3728e

Browse files
committed
Change: Adjust default logging configuration
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. (cherry picked from commit 52ef7a0)
1 parent 65bd946 commit ab3728e

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

ospd/logger.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
# You should have received a copy of the GNU Affero General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import configparser
1819
import logging
1920
import os
20-
import configparser
2121
import time
22-
2322
from logging.config import fileConfig
2423
from pathlib import Path
2524
from typing import Optional
2625

27-
2826
DEFAULT_HANDLER_CONSOLE = {
2927
'class': 'logging.StreamHandler',
3028
'level': 'INFO',
@@ -33,9 +31,10 @@
3331
}
3432

3533
DEFAULT_HANDLER_FILE = {
36-
'class': 'FileHandler',
34+
'class': 'handlers.WatchedFileHandler',
3735
'level': 'INFO',
3836
'formatter': 'file',
37+
'args': '("/dev/null", "a")',
3938
}
4039

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

48-
DEFAULT_HANDLERS = {'keys': 'default_handler'}
47+
DEFAULT_HANDLERS = {'keys': 'console,file,syslog'}
4948
DEFAULT_FORMATTERS = {'keys': 'file,syslog'}
5049
DEFAULT_FORMATTER_FILE = {
51-
'format': 'OSPD['
52-
+ str(os.getpid())
53-
+ '] %(asctime)s: %(levelname)s: (%(name)s) %(message)s',
50+
'format': f'OSPD[{os.getpid()}] %(asctime)s: %(levelname)s: '
51+
'(%(name)s) %(message)s',
5452
'datefmt': '',
5553
}
5654
DEFAULT_FORMATTER_SYSLOG = {
57-
'format': 'OSPD['
58-
+ str(os.getpid())
59-
+ '] %(levelname)s: (%(name)s) %(message)s',
55+
'format': f'OSPD[{os.getpid()}] %(levelname)s: (%(name)s) %(message)s',
6056
'datefmt': '',
6157
}
6258
DEFAULT_LOGGERS = {'keys': 'root'}
6359
DEFAULT_ROOT_LOGGER = {
6460
'level': 'NOTSET',
65-
'handlers': 'default_handler',
61+
'handlers': 'file',
6662
'propagate': '0',
6763
}
6864

@@ -73,28 +69,40 @@ def init_logging(
7369
log_file: Optional[str] = None,
7470
log_config: Optional[str] = None,
7571
foreground: Optional[bool] = False,
76-
):
72+
) -> None:
7773
config = configparser.ConfigParser()
7874
config['handlers'] = DEFAULT_HANDLERS
7975
config['formatters'] = DEFAULT_FORMATTERS
8076
config['formatter_file'] = DEFAULT_FORMATTER_FILE
8177
config['formatter_syslog'] = DEFAULT_FORMATTER_SYSLOG
78+
config['handler_console'] = DEFAULT_HANDLER_CONSOLE
79+
config['handler_syslog'] = DEFAULT_HANDLER_SYSLOG
80+
config['handler_file'] = DEFAULT_HANDLER_FILE
81+
config['loggers'] = DEFAULT_LOGGERS
82+
config['logger_root'] = DEFAULT_ROOT_LOGGER
8283

8384
if foreground:
84-
config['handler_default_handler'] = DEFAULT_HANDLER_CONSOLE
85-
elif log_file:
86-
config['handler_default_handler'] = DEFAULT_HANDLER_FILE
87-
config['handler_default_handler']['args'] = "('" + log_file + "', 'a')"
88-
else:
89-
config['handler_default_handler'] = DEFAULT_HANDLER_SYSLOG
85+
config['logger_root']['handlers'] = 'console'
86+
87+
if log_file:
88+
if foreground:
89+
config['logger_root']['handlers'] = 'console,file'
90+
else:
91+
config['logger_root']['handlers'] = 'file'
92+
93+
config['handler_file']['args'] = f"('{log_file}', 'a')"
94+
95+
if not foreground and not log_file:
96+
config['logger_root']['handlers'] = 'syslog'
97+
98+
config['handler_file']['level'] = log_level
99+
config['handler_console']['level'] = log_level
100+
config['handler_syslog']['level'] = log_level
90101

91-
config['handler_default_handler']['level'] = log_level
92102
log_config_path = Path(log_config)
103+
93104
if log_config_path.exists():
94105
config.read(log_config)
95-
else:
96-
config['loggers'] = DEFAULT_LOGGERS
97-
config['logger_root'] = DEFAULT_ROOT_LOGGER
98106

99107
fileConfig(config, disable_existing_loggers=False)
100108
logging.Formatter.converter = time.gmtime

0 commit comments

Comments
 (0)