Coverage for pygeoapi/log.py: 46%
29 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 08:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 08:15 +0000
1# =================================================================
2#
3# Authors: Tom Kralidis <tomkralidis@gmail.com>
4#
5# Copyright (c) 2023 Tom Kralidis
6#
7# Permission is hereby granted, free of charge, to any person
8# obtaining a copy of this software and associated documentation
9# files (the "Software"), to deal in the Software without
10# restriction, including without limitation the rights to use,
11# copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the
13# Software is furnished to do so, subject to the following
14# conditions:
15#
16# The above copyright notice and this permission notice shall be
17# included in all copies or substantial portions of the Software.
18#
19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26# OTHER DEALINGS IN THE SOFTWARE.
27#
28# =================================================================
30"""Logging system"""
32import logging
33from logging.handlers import RotatingFileHandler
34from logging.handlers import TimedRotatingFileHandler
35import sys
37LOGGER = logging.getLogger(__name__)
40def setup_logger(logging_config: dict):
41 """
42 Setup configuration
44 :param logging_config: logging specific configuration
46 :returns: void (creates logging instance)
47 """
49 default_log_format = (
50 '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s'
51 )
52 default_date_format = '%Y-%m-%dT%H:%M:%SZ'
54 log_format = logging_config.get('logformat', default_log_format)
55 date_format = logging_config.get('dateformat', default_date_format)
57 loglevels = {
58 'CRITICAL': logging.CRITICAL,
59 'ERROR': logging.ERROR,
60 'WARNING': logging.WARNING,
61 'INFO': logging.INFO,
62 'DEBUG': logging.DEBUG,
63 'NOTSET': logging.NOTSET
64 }
66 loglevel = loglevels[logging_config['level']]
68 if 'logfile' in logging_config: 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true
69 rotation = logging_config.get('rotation')
70 if rotation:
71 rotate_mode = rotation.get('mode')
73 rotate_backup_count = rotation.get('backup_count', 0)
75 if rotate_mode == 'size':
76 rotate_max_bytes = rotation.get('max_bytes', 0)
78 logging.basicConfig(
79 handlers=[
80 RotatingFileHandler(
81 filename=logging_config['logfile'],
82 maxBytes=rotate_max_bytes,
83 backupCount=rotate_backup_count
84 )
85 ],
86 level=loglevel,
87 datefmt=date_format,
88 format=log_format,
89 )
90 elif rotate_mode == 'time':
91 rotate_when = rotation.get('when', 'h')
92 rotate_interval = rotation.get('interval', 1)
94 logging.basicConfig(
95 handlers=[
96 TimedRotatingFileHandler(
97 filename=logging_config['logfile'],
98 when=rotate_when,
99 interval=rotate_interval,
100 backupCount=rotate_backup_count
101 )
102 ],
103 level=loglevel,
104 datefmt=date_format,
105 format=log_format
106 )
107 else:
108 raise Exception(f'Invalid rotation mode:{rotate_mode}')
109 else:
110 logging.basicConfig(
111 level=loglevel,
112 datefmt=date_format,
113 format=log_format,
114 filename=logging_config['logfile']
115 )
116 else:
117 logging.basicConfig(
118 level=loglevel,
119 datefmt=date_format,
120 format=log_format,
121 stream=sys.stdout
122 )
124 LOGGER.debug('Logging initialized')
125 return