-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from svalinn/null-logger
- Loading branch information
Showing
5 changed files
with
114 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,69 @@ | ||
import logging | ||
import time | ||
|
||
|
||
def check_init(logger_obj): | ||
"""Checks if a logger object has been instantiated, and if not, | ||
instantiates one. | ||
Arguments: | ||
logger_obj (object or None): logger object input. | ||
Returns: | ||
logger_obj (object): logger object. | ||
""" | ||
if logger_obj != None and logger_obj.hasHandlers(): | ||
return logger_obj | ||
else: | ||
return NullLogger() | ||
|
||
|
||
class NullLogger(object): | ||
"""Creates a pseudo logger object mimicking an actual logger object whose | ||
methods do nothing when called. | ||
""" | ||
def __init__(self): | ||
pass | ||
|
||
def hasHandlers(self): | ||
return True | ||
|
||
def info(self, message): | ||
current_time = time.localtime() | ||
current_time = time.strftime('%H:%M:%S', current_time) | ||
print(f'{current_time}: {message}') | ||
|
||
def warning(self, *args): | ||
pass | ||
|
||
def error(self, *args): | ||
pass | ||
|
||
|
||
def init(): | ||
"""Creates and configures logger with separate stream and file handlers. | ||
Returns: | ||
logger (object): logger object. | ||
""" | ||
# Create logger | ||
logger = logging.getLogger('log') | ||
# Configure base logger message level | ||
|
||
logger.setLevel(logging.INFO) | ||
# Configure stream handler | ||
|
||
s_handler = logging.StreamHandler() | ||
# Configure file handler | ||
f_handler = logging.FileHandler('stellarator.log') | ||
# Define and set logging format | ||
f_handler = logging.FileHandler( | ||
filename='stellarator.log', | ||
mode='w' | ||
) | ||
|
||
format = logging.Formatter( | ||
fmt = '%(asctime)s: %(message)s', | ||
datefmt = '%H:%M:%S' | ||
) | ||
s_handler.setFormatter(format) | ||
f_handler.setFormatter(format) | ||
# Add handlers to logger | ||
|
||
logger.addHandler(s_handler) | ||
logger.addHandler(f_handler) | ||
|
||
return logger | ||
|
||
|
||
def check_init(logger_obj): | ||
"""Checks if a logger object has been instantiated, and if not, | ||
instantiates one. | ||
Arguments: | ||
logger_obj (object or None): logger object input. | ||
Returns: | ||
logger_obj (object): logger object. | ||
""" | ||
if logger_obj != None and logger_obj.hasHandlers(): | ||
return logger_obj | ||
else: | ||
return init() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters