Skip to content

Commit 1b09155

Browse files
committed
Add init method to logger to make sure we can pass arguments on init
1 parent 5c63cc8 commit 1b09155

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

  • {{cookiecutter.repo_name}}/python/{{cookiecutter.package_name|lower}}/utils

{{cookiecutter.repo_name}}/python/{{cookiecutter.package_name|lower}}/utils/logger.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,11 @@ class SDSSLogger(logging.Logger):
107107
108108
"""
109109

110-
def __init__(self, name, log_level=logging.INFO, capture_warnings=True):
110+
def __init__(self, name):
111111

112112
super(SDSSLogger, self).__init__(name)
113113

114-
if name == 'py.warnings':
115-
return
114+
def init(self, log_level=logging.INFO, capture_warnings=True):
116115

117116
# Set levels
118117
self.setLevel(logging.DEBUG)
@@ -131,30 +130,25 @@ def __init__(self, name, log_level=logging.INFO, capture_warnings=True):
131130
sys.excepthook = self._catch_exceptions
132131

133132
self.warnings_logger = None
134-
self.capture_warnings(capture_warnings)
133+
134+
if capture_warnings:
135+
self.capture_warnings()
135136

136137
def _catch_exceptions(self, exctype, value, tb):
137138
"""Catches all exceptions and logs them."""
138139

139140
self.error(get_exception_formatted(exctype, value, tb))
140141

141-
def capture_warnings(self, action):
142+
def capture_warnings(self):
142143
"""Capture warnings.
143144
144-
If ``action`` is `True`, redirects all the warnings to a logger called
145-
``py.warnings``. Handlers are added to that logger. If ``action=False``
146-
disable the warning capture.
145+
When `logging.captureWarnings` is `True`, all the warnings are
146+
redirected to a logger called ``py.warnings``. We add our handlers
147+
to the warning logger.
147148
148149
"""
149150

150-
if action is False:
151-
logging.captureWarnings(action)
152-
self.warnings_logger = None
153-
return
154-
155-
assert self.sh is not None, 'shell handler must'
156-
157-
logging.captureWarnings(action)
151+
logging.captureWarnings(True)
158152

159153
self.warnings_logger = logging.getLogger('py.warnings')
160154
self.warnings_logger.addHandler(self.sh)
@@ -208,14 +202,15 @@ def set_level(self, level):
208202
self.fh.setLevel(level)
209203

210204

211-
def get_logger(name):
205+
def get_logger(name, **kwargs):
212206
"""Gets a new logger."""
213207

214208
orig_logger = logging.getLoggerClass()
215209

216210
logging.setLoggerClass(SDSSLogger)
217211

218212
log = logging.getLogger(name)
213+
log.init(**kwargs)
219214

220215
logging.setLoggerClass(orig_logger)
221216

0 commit comments

Comments
 (0)