Skip to content

Instantly share code, notes, and snippets.

@mjtiempo
Last active October 2, 2022 02:01
Show Gist options
  • Save mjtiempo/6c02ffdaa8bb87c4e91f6e51e2ff7b17 to your computer and use it in GitHub Desktop.
Save mjtiempo/6c02ffdaa8bb87c4e91f6e51e2ff7b17 to your computer and use it in GitHub Desktop.
Logging to stdout and file in python
'''
src: https://stackoverflow.com/a/46098711/2433866
'''
import sys
import logging
import logging.handlers
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
handlers=[
logging.handlers.RotatingFileHandler(
'main.log',
maxBytes=(1048576*5),
backupCount=9
),
logging.StreamHandler(sys.stdout)
]
)
''' test below '''
mainLogger = logging.getLogger('mainLogger')
newLogger = logging.getLogger('newLogger')
for i in range(100000):
mainLogger.info(f"mainLogger {i}");
newLogger.info(f"newLogger! {i}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment