Created
June 20, 2020 13:22
-
-
Save panamantis/5797dda98b1fa6fab2f739a7aacc5e9d to your computer and use it in GitHub Desktop.
Python logging configuration files (yaml, json, ini)
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
[loggers] | |
keys=root,app,uvicorn | |
[handlers] | |
keys=console,file,uvicorn | |
[formatters] | |
keys=console,file | |
[logger_root] | |
level=DEBUG | |
handlers=console | |
[logger_app] | |
level=INFO | |
handlers=file | |
propagate=1 | |
qualname=app | |
[logger_uvicorn] | |
level=INFO | |
handlers=uvicorn | |
propagate=1 | |
qualname=uvicorn | |
[handler_console] | |
class=StreamHandler | |
level=DEBUG | |
formatter=console | |
args=(sys.stdout,) | |
[handler_file] | |
class=logging.handlers.TimedRotatingFileHandler | |
level=DEBUG | |
formatter=file | |
args=(os.getcwd() + '/logs/training-stats.log', "D", 1, 0) | |
[handler_uvicorn] | |
class=logging.handlers.TimedRotatingFileHandler | |
level=DEBUG | |
formatter=file | |
args=(os.getcwd() + '/logs/uvicorn.log', "D", 1, 0) | |
[formatter_console] | |
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s | |
[formatter_file] | |
format=%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)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
{ | |
"version": 1, | |
"disable_existing_loggers": false, | |
"formatters": { | |
"simple": { | |
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
}, | |
"verbose": { | |
"format": "%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s" | |
} | |
}, | |
"handlers": { | |
"console": { | |
"class": "logging.StreamHandler", | |
"level": "DEBUG", | |
"formatter": "simple", | |
"stream": "ext://sys.stdout" | |
}, | |
"file": { | |
"class": "logging.handlers.TimedRotatingFileHandler", | |
"level": "DEBUG", | |
"formatter": "verbose", | |
"when": "D", | |
"backupCount": 0, | |
"filename": "./logs/training-stats.log" | |
}, | |
"uvicorn": { | |
"class": "logging.handlers.TimedRotatingFileHandler", | |
"level": "DEBUG", | |
"formatter": "verbose", | |
"when": "D", | |
"backupCount": 0, | |
"filename": "./logs/uvicorn.log" | |
} | |
}, | |
"loggers": { | |
"root": { | |
"level": "DEBUG", | |
"handlers": ["console"] | |
}, | |
"app": { | |
"level": "DEBUG", | |
"handlers": ["file"], | |
"propagate": true, | |
"qualname": "app" | |
}, | |
"uvicorn": { | |
"level": "DEBUG", | |
"handlers": ["uvicorn"], | |
"propagate": true, | |
"qualname": "uvicorn" | |
} | |
} | |
} |
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
version: 1 | |
disable_existing_loggers: no | |
formatters: | |
simple: | |
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
verbose: | |
format: "%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s" | |
handlers: | |
console: | |
class: logging.StreamHandler | |
level: DEBUG | |
formatter: simple | |
stream: ext://sys.stdout | |
file: | |
class: logging.handlers.TimedRotatingFileHandler | |
level: DEBUG | |
formatter: verbose | |
when: D | |
backupCount: 0 | |
filename: ./logs/training-stats.log | |
uvicorn: | |
class: logging.handlers.TimedRotatingFileHandler | |
level: DEBUG | |
formatter: verbose | |
when: D | |
backupCount: 0 | |
filename: ./logs/uvicorn.log | |
loggers: | |
root: | |
level: DEBUG | |
handlers: [console] | |
app: | |
level: DEBUG | |
handlers: [file] | |
propagate: yes | |
qualname: app | |
uvicorn: | |
level: INFO | |
handlers: [uvicorn] | |
propagate: yes | |
qualname: uvicorn |
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 os | |
import sys | |
import json | |
import logging.config | |
import yaml | |
def setup_yaml(): | |
with open("logging.yaml", "r") as f: | |
yaml_config = yaml.safe_load(f.read()) | |
logging.config.dictConfig(yaml_config) | |
def setup_json(): | |
with open("logging.json", "r") as f: | |
json_config = json.load(f) | |
logging.config.dictConfig(config) | |
def setup_ini(): | |
logging.config.fileConfig("logging.ini") | |
def main(): | |
format = sys.argv[0] | |
if format == "json": | |
setup_json() | |
elif format == "yaml": | |
setup_yaml() | |
elif format == "ini": | |
setup_ini() | |
else: | |
print("File format does not exist") | |
sys.exit() | |
if __name__ == "__main__": | |
main() | |
logger = logging.getLogger(__name__) | |
logger.info("hello world") | |
logger.error("fatal error") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
--- Logging error ---
Traceback (most recent call last):
File "D:\PYTHON SETUP\lib\logging\handlers.py", line 70, in emit
self.doRollover()
File "D:\PYTHON SETUP\lib\logging\handlers.py", line 394, in doRollover
self.rotate(self.baseFilename, dfn)
File "D:\PYTHON SETUP\lib\logging\handlers.py", line 111, in rotate
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:\PYTHON\QTASolution\Backend\logs\QTASolutions.log' -> 'D:\PYTHON\QTASolution\Backend\logs\QTASolutions.log.2022-03-24_10_52_53.log'
Call stack:
File "D:\PYTHON SETUP\lib\threading.py", line 890, in _bootstrap
self._bootstrap_inner()
File "D:\PYTHON SETUP\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "D:\PYTHON SETUP\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\PYTHON SETUP\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "D:\PYTHON SETUP\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "D:\PYTHON SETUP\lib\socketserver.py", line 720, in init
self.handle()
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 347, in handle
BaseHTTPRequestHandler.handle(self)
File "D:\PYTHON SETUP\lib\http\server.py", line 426, in handle
self.handle_one_request()
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 379, in handle_one_request
self.run_wsgi()
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 324, in run_wsgi
execute(self.server.app)
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 316, in execute
write(data)
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 274, in write
self.send_response(code, msg)
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 383, in send_response
self.log_request(code)
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 433, in log_request
self.log("info", '"%s" %s %s', msg, code, size)
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug\serving.py", line 442, in log
_log(
File "D:\PYTHON\QTASolution\Backend\env\lib\site-packages\werkzeug_internal.py", line 225, in _log
getattr(_logger, type)(message.rstrip(), *args, **kwargs)
Message: '192.168.100.143 - - [24/Mar/2022 10:54:30] "%s" %s %s'