Skip to content

Commit 175d8a7

Browse files
committed
Correct Appending of Log Handlers
The current way logging is setup is that it appends a handler logging.getHandler('') on each __init__ of DashboardAPI() (and async classes). I replicated the way logging is set in the BasicConfig currently to be set to the logger instance at the module level. This will allow it to function cleaner when included in applications that have already configured the root logger. The log levels are interesting. The logger level is set to DEBUG and pulled down to INFO for console logging when a file is also being logged to. If no file is specified, the log level is kept at DEBUG. This holds true in this patch so it can be reviewed in future changes. Notably, because the level is set to DEBUG in logger and not in the root logger, the log file (or console with no file) will not show DEBUG messages from other code (an example would be urllib3).
1 parent 100e50a commit 175d8a7

4 files changed

Lines changed: 88 additions & 64 deletions

File tree

meraki/__init__.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,32 @@ def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeo
6969
# Configure logging
7070
if not suppress_logging:
7171
self._logger = logging.getLogger(__name__)
72+
self._logger.setLevel(logging.DEBUG)
73+
74+
formatter = logging.Formatter(
75+
fmt='%(name)12s: %(levelname)8s > %(message)s',
76+
datefmt='%Y-%m-%d %H:%M:%S'
77+
)
78+
7279
if log_path and log_path[-1] != '/':
7380
log_path += '/'
7481
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
75-
if output_log:
76-
logging.basicConfig(
77-
filename=self._log_file,
78-
level=logging.DEBUG,
79-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
80-
datefmt='%Y-%m-%d %H:%M:%S')
82+
83+
handler_console = logging.StreamHandler()
84+
handler_log = logging.FileHandler(
85+
filename=self._log_file
86+
)
87+
88+
handler_console.setFormatter(formatter)
89+
handler_log.setFormatter(formatter)
90+
91+
if output_log and not self._logger.hasHandlers():
92+
self._logger.addHandler(handler_log)
8193
if print_console:
82-
console = logging.StreamHandler()
83-
console.setLevel(logging.INFO)
84-
formatter = logging.Formatter('%(name)12s: %(levelname)8s > %(message)s')
85-
console.setFormatter(formatter)
86-
logging.getLogger('').addHandler(console)
87-
elif print_console:
88-
logging.basicConfig(
89-
level=logging.DEBUG,
90-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
91-
datefmt='%Y-%m-%d %H:%M:%S')
94+
handler_console.setLevel(logging.INFO)
95+
self._logger.addHandler(handler_console)
96+
elif print_console and not self._logger.hasHandlers():
97+
self._logger.addHandler(handler_console)
9298
else:
9399
self._logger = None
94100

meraki/aio/__init__.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,32 @@ def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeo
6969
# Configure logging
7070
if not suppress_logging:
7171
self._logger = logging.getLogger(__name__)
72+
self._logger.setLevel(logging.DEBUG)
73+
74+
formatter = logging.Formatter(
75+
fmt='%(name)12s: %(levelname)8s > %(message)s',
76+
datefmt='%Y-%m-%d %H:%M:%S'
77+
)
78+
7279
if log_path and log_path[-1] != '/':
7380
log_path += '/'
7481
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
75-
if output_log:
76-
logging.basicConfig(
77-
filename=self._log_file,
78-
level=logging.DEBUG,
79-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
80-
datefmt='%Y-%m-%d %H:%M:%S')
82+
83+
handler_console = logging.StreamHandler()
84+
handler_log = logging.FileHandler(
85+
filename=self._log_file
86+
)
87+
88+
handler_console.setFormatter(formatter)
89+
handler_log.setFormatter(formatter)
90+
91+
if output_log and not self._logger.hasHandlers():
92+
self._logger.addHandler(handler_log)
8193
if print_console:
82-
console = logging.StreamHandler()
83-
console.setLevel(logging.INFO)
84-
formatter = logging.Formatter('%(name)12s: %(levelname)8s > %(message)s')
85-
console.setFormatter(formatter)
86-
logging.getLogger('').addHandler(console)
87-
elif print_console:
88-
logging.basicConfig(
89-
level=logging.DEBUG,
90-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
91-
datefmt='%Y-%m-%d %H:%M:%S')
94+
handler_console.setLevel(logging.INFO)
95+
self._logger.addHandler(handler_console)
96+
elif print_console and not self._logger.hasHandlers():
97+
self._logger.addHandler(handler_console)
9298
else:
9399
self._logger = None
94100

meraki_v0/__init__.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,32 @@ def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeo
138138
# Configure logging
139139
if not suppress_logging:
140140
self._logger = logging.getLogger(__name__)
141+
self._logger.setLevel(logging.DEBUG)
142+
143+
formatter = logging.Formatter(
144+
fmt='%(name)12s: %(levelname)8s > %(message)s',
145+
datefmt='%Y-%m-%d %H:%M:%S'
146+
)
147+
141148
if log_path and log_path[-1] != '/':
142149
log_path += '/'
143150
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
144-
if output_log:
145-
logging.basicConfig(
146-
filename=self._log_file,
147-
level=logging.DEBUG,
148-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
149-
datefmt='%Y-%m-%d %H:%M:%S')
151+
152+
handler_console = logging.StreamHandler()
153+
handler_log = logging.FileHandler(
154+
filename=self._log_file
155+
)
156+
157+
handler_console.setFormatter(formatter)
158+
handler_log.setFormatter(formatter)
159+
160+
if output_log and not self._logger.hasHandlers():
161+
self._logger.addHandler(handler_log)
150162
if print_console:
151-
console = logging.StreamHandler()
152-
console.setLevel(logging.INFO)
153-
formatter = logging.Formatter('%(name)12s: %(levelname)8s > %(message)s')
154-
console.setFormatter(formatter)
155-
logging.getLogger('').addHandler(console)
156-
elif print_console:
157-
logging.basicConfig(
158-
level=logging.DEBUG,
159-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
160-
datefmt='%Y-%m-%d %H:%M:%S')
163+
handler_console.setLevel(logging.INFO)
164+
self._logger.addHandler(handler_console)
165+
elif print_console and not self._logger.hasHandlers():
166+
self._logger.addHandler(handler_console)
161167
else:
162168
self._logger = None
163169

meraki_v0/aio/__init__.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,32 @@ def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeo
135135
# Configure logging
136136
if not suppress_logging:
137137
self._logger = logging.getLogger(__name__)
138+
self._logger.setLevel(logging.DEBUG)
139+
140+
formatter = logging.Formatter(
141+
fmt='%(name)12s: %(levelname)8s > %(message)s',
142+
datefmt='%Y-%m-%d %H:%M:%S'
143+
)
144+
138145
if log_path and log_path[-1] != '/':
139146
log_path += '/'
140147
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
141-
if output_log:
142-
logging.basicConfig(
143-
filename=self._log_file,
144-
level=logging.DEBUG,
145-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
146-
datefmt='%Y-%m-%d %H:%M:%S')
148+
149+
handler_console = logging.StreamHandler()
150+
handler_log = logging.FileHandler(
151+
filename=self._log_file
152+
)
153+
154+
handler_console.setFormatter(formatter)
155+
handler_log.setFormatter(formatter)
156+
157+
if output_log and not self._logger.hasHandlers():
158+
self._logger.addHandler(handler_log)
147159
if print_console:
148-
console = logging.StreamHandler()
149-
console.setLevel(logging.INFO)
150-
formatter = logging.Formatter('%(name)12s: %(levelname)8s > %(message)s')
151-
console.setFormatter(formatter)
152-
logging.getLogger('').addHandler(console)
153-
elif print_console:
154-
logging.basicConfig(
155-
level=logging.DEBUG,
156-
format='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
157-
datefmt='%Y-%m-%d %H:%M:%S')
160+
handler_console.setLevel(logging.INFO)
161+
self._logger.addHandler(handler_console)
162+
elif print_console and not self._logger.hasHandlers():
163+
self._logger.addHandler(handler_console)
158164
else:
159165
self._logger = None
160166

0 commit comments

Comments
 (0)