Skip to content

Instantly share code, notes, and snippets.

@wassname
Last active November 11, 2022 10:11
Show Gist options
  • Save wassname/d17325f36c36fa663dd7de3c09a55e74 to your computer and use it in GitHub Desktop.
Save wassname/d17325f36c36fa663dd7de3c09a55e74 to your computer and use it in GitHub Desktop.
simple logging for jupyter or python which outputs to stdout (or a console or terminal) and a log file
"""
In jupyter notebook simple logging to console
"""
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# Test
logger = logging.getLogger('LOGGER_NAME')
logger.debug('This is hidden')
logger.info('So this is shown on the console')
logger.warning('This too')
"""
In jupyter notebook simple logging to console and file:
"""
import logging
import sys
logging.basicConfig(
level=logging.INFO,
format='[{%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(filename='tmp5a.log'),
logging.StreamHandler(sys.stdout)
]
)
# Test
logger = logging.getLogger('LOGGER_NAME')
logger.debug('This message should go to the log file and to the console')
logger.info('So should this')
logger.warning('And this, too')
"""
Setup simple logging in python. This logs info message to stdout and debug messages to file.
Sure it's long but this is as simple as I could make it for this outcome.
Note: We must set the root logger at DEBUG level, since it must be higher than it's children to pass them on.
Then set filehandler at debug and stream handler at info.
"""
import logging
import sys
import datetime
# To use differen't log level for file and console
timestamp = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S')
filename=f'/tmp/tmp5a_{timestamp}.log'
formatter = logging.Formatter('[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')
file_handler = logging.FileHandler(filename=filename)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.INFO)
# The handlers have to be at a root level since they are the final output
logging.basicConfig(
level=logging.DEBUG,
format='[{%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
handlers=[
file_handler,
stream_handler
]
)
# Test
logger = logging.getLogger('LOGGER_NAME')
logger.debug('This message should go to the log file')
logger.info('This should go to the stdout and file')
logger.warning('And this, too')
print('filelog', open(filename).read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment