Skip to content

Commit

Permalink
#464 add_logger function in its original version
Browse files Browse the repository at this point in the history
  • Loading branch information
aanersc committed Mar 4, 2021
1 parent 99c243a commit 335e5ce
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions nansat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
from __future__ import absolute_import

import os
import os, sys
import warnings
import logging
from dateutil.parser import parse
Expand Down Expand Up @@ -196,13 +196,25 @@ def add_logger(logName='', logLevel=None):
"""
if logLevel is not None:
os.environ['LOG_LEVEL'] = str(logLevel)

# create (or take already existing) logger
# with default logging level WARNING
logger = logging.getLogger(logName)
logger.setLevel(int(os.environ['LOG_LEVEL']))

# if logger already exits, default stream handler has been already added
# otherwise create and add a new handler
if len(logger.handlers) == 0:
logger.addHandler(logging.NullHandler())
logger.handlers[0].setLevel(int(os.environ['LOG_LEVEL']))
logger.setLevel(int(os.environ['LOG_LEVEL']))
# create console handler and set level to debug
ch = logging.StreamHandler(sys.stdout)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(threadName)s - '
'%(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)

logger.handlers[0].setLevel(int(os.environ['LOG_LEVEL']))

return logger

Expand Down

0 comments on commit 335e5ce

Please sign in to comment.