Skip to content

Commit

Permalink
Set up logs before global config
Browse files Browse the repository at this point in the history
  • Loading branch information
jpgill86 committed Dec 26, 2020
1 parent 9d3f316 commit bc6de9d
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions neurotic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,59 @@
if not os.path.exists(neurotic_dir):
os.mkdir(neurotic_dir)


class FileLoggingFormatter(logging.Formatter):
"""
A custom formatter for file logging
"""
default_msec_format = '%s.%03d' # use period radix point instead of comma in decimal seconds
def format(self, record):
if logging.getLogger(__name__).level <= logging.DEBUG:
# include more detail if the logger level (not the record level) is
# debug or lower
self._style._fmt = '[%(asctime)s] [%(levelname)-8s] [%(threadName)-10s] [%(name)s:%(lineno)d (%(funcName)s)] %(message)s'
else:
self._style._fmt = '[%(asctime)s] [%(levelname)-8s] %(message)s'
return super().format(record)

class StreamLoggingFormatter(logging.Formatter):
"""
A custom formatter for stream logging
"""
def format(self, record):
if record.levelno == logging.INFO:
# exclude the level name ("INFO") from common log records
self._style._fmt = '[neurotic] %(message)s'
else:
self._style._fmt = '[neurotic] %(levelname)s: %(message)s'
return super().format(record)


# set the file path for logging
log_file = os.path.join(neurotic_dir, 'neurotic-log.txt')

# set the default level for logging to INFO unless it was set to a custom level
# before importing the package
logger = logging.getLogger(__name__)
if logger.level == logging.NOTSET:
default_log_level = logging.INFO
logger.setLevel(default_log_level)
else:
default_log_level = logger.level

# write log records to a file, rotating files if it exceeds 10 MB
logger_filehandler = logging.handlers.RotatingFileHandler(filename=log_file, maxBytes=10000000, backupCount=2)
logger_filehandler.setFormatter(FileLoggingFormatter())
logger.addHandler(logger_filehandler)
logger.info('===========================') # file logger only
logger.info(f'Importing neurotic {__version__}') # file logger only

# stream log records to stderr
logger_streamhandler = logging.StreamHandler(stream=sys.stderr)
logger_streamhandler.setFormatter(StreamLoggingFormatter())
logger.addHandler(logger_streamhandler)


global_config = {
'defaults': {
# defaults used by the command line interface
Expand Down Expand Up @@ -89,59 +142,6 @@ def update_global_config_from_file(file=global_config_file):
with open(file, 'r') as f:
update_dict(global_config, toml.loads(f.read()))


class FileLoggingFormatter(logging.Formatter):
"""
A custom formatter for file logging
"""
default_msec_format = '%s.%03d' # use period radix point instead of comma in decimal seconds
def format(self, record):
if logging.getLogger(__name__).level <= logging.DEBUG:
# include more detail if the logger level (not the record level) is
# debug or lower
self._style._fmt = '[%(asctime)s] [%(levelname)-8s] [%(threadName)-10s] [%(name)s:%(lineno)d (%(funcName)s)] %(message)s'
else:
self._style._fmt = '[%(asctime)s] [%(levelname)-8s] %(message)s'
return super().format(record)

class StreamLoggingFormatter(logging.Formatter):
"""
A custom formatter for stream logging
"""
def format(self, record):
if record.levelno == logging.INFO:
# exclude the level name ("INFO") from common log records
self._style._fmt = '[neurotic] %(message)s'
else:
self._style._fmt = '[neurotic] %(levelname)s: %(message)s'
return super().format(record)


# set the file path for logging
log_file = os.path.join(neurotic_dir, 'neurotic-log.txt')

# set the default level for logging to INFO unless it was set to a custom level
# before importing the package
logger = logging.getLogger(__name__)
if logger.level == logging.NOTSET:
default_log_level = logging.INFO
logger.setLevel(default_log_level)
else:
default_log_level = logger.level

# write log records to a file, rotating files if it exceeds 10 MB
logger_filehandler = logging.handlers.RotatingFileHandler(filename=log_file, maxBytes=10000000, backupCount=2)
logger_filehandler.setFormatter(FileLoggingFormatter())
logger.addHandler(logger_filehandler)
logger.info('===========================') # file logger only
logger.info(f'Importing neurotic {__version__}') # file logger only

# stream log records to stderr
logger_streamhandler = logging.StreamHandler(stream=sys.stderr)
logger_streamhandler.setFormatter(StreamLoggingFormatter())
logger.addHandler(logger_streamhandler)


try:
update_global_config_from_file()
except Exception as e:
Expand Down

0 comments on commit bc6de9d

Please sign in to comment.