Last active
September 28, 2020 14:05
-
-
Save Integralist/0f088983d522de5c0c81ea148823eef1 to your computer and use it in GitHub Desktop.
[Python3 Logging] Simple Python3 Logging Configuration #logs #python #python3 #structured #logging #structlog #capture
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
import io | |
import logging | |
logger = logging.getLogger("your_logger") | |
logger.setLevel(logging.INFO) | |
logger_output = io.StringIO() | |
logger.addHandler(logging.StreamHandler(logger_output)) | |
# do stuff that triggers some logs | |
logger_contents = logger_output.getvalue() | |
logger_output.close() | |
print(logger_contents) |
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
# https://docs.python.org/3/library/logging.html#logrecord-attributes | |
import logging | |
log = logging.getLogger() # <logging.RootLogger at 0x107f72f98> | |
log.setLevel(logging.DEBUG) | |
log.debug('123') # DEBUG:root:123 | |
log.info('456') # INFO:root:456 | |
# Alternatively, you can default to the 'root' logger implicitly, | |
# but you can't use setLevel as that's only available on the logger instance, | |
# and a logger instance is what you're getting with getLogger. | |
# so instead you use basicConfig... | |
import logging | |
logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s') | |
# A format I like... | |
import logging | |
logging.basicConfig( | |
level=logging.INFO, | |
format='[%(levelname)s %(asctime)s path:%(pathname)s lineno:%(lineno)s] %(message)s', | |
datefmt='%Y/%m/%d %I:%M:%S' | |
) |
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
# Keep logs quiet (so only critical messages are shown, not INFO messages) | |
logging.getLogger("boto").setLevel(logging.CRITICAL) | |
logging.getLogger("nsq").setLevel(logging.CRITICAL) |
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
import logging | |
import structlog | |
# create logger | |
logger = logging.getLogger('simple_example') | |
logger.setLevel(logging.DEBUG) | |
# We need factory, to return application-wide logger | |
def logger_factory(): | |
return logger | |
structlog.configure( | |
processors=[ | |
structlog.processors.JSONRenderer(indent=2, sort_keys=True) | |
], | |
logger_factory=logger_factory | |
) | |
log = structlog.getLogger() | |
log.debug("Now you see me") | |
logger.setLevel(logging.ERROR) | |
log.debug("Now you don't") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment