Created
April 4, 2019 08:16
-
-
Save rawlik/559c5dddd0ce3eb4307c0f2a9e5ff5b9 to your computer and use it in GitHub Desktop.
Python logging in multiple modules
This file contains hidden or 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 coloredlogs | |
import logging | |
if __name__=="__main__": | |
# only configure logging in the main program, everywhere else just import and use it | |
# set up logging | |
logfile_folder = "N:/data/log/" | |
logfile_date = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | |
logfile_filename = f"bunker5_{logfile_date}.log" | |
logfile_full = os.path.join(logfile_folder, logfile_filename) | |
log_format = "%(asctime)s %(module)-15s %(levelname)-8s %(message)s" | |
logging.basicConfig( | |
filename=logfile_full, | |
level=logging.DEBUG, | |
format=log_format) | |
coloredlogs.DEFAULT_LOG_FORMAT = log_format | |
coloredlogs.DEFAULT_FIELD_STYLES["module"] = {"color": "magenta"} | |
coloredlogs.DEFAULT_FIELD_STYLES["asctime"] = {"color": "blue", "faint": "true"} | |
coloredlogs.DEFAULT_FIELD_STYLES["levelname"] = {"color": "cyan"} | |
coloredlogs.DEFAULT_LEVEL_STYLES["debug"] = {"color": "black", "bright": "true"} | |
coloredlogs.install(milliseconds=True) | |
coloredlogs.set_level(args.loglevel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment