Answer the question
In order to leave comments, you need to log in
Why does logging immediately output both to the file and to the console (configured via dictconfig)?
Why are the logs output both to the file and to the console, and is it possible to somehow remove it, I want it to be output to the console and to the file when necessary, and when it is not necessary, it is output only to the file.
in general, there is such a config:
version: 1
disable_existing_loggers: false
formatters:
standard:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
error:
format: "%(levelname)s <PID %(process)d:%(processName)s> %(name)s.%(funcName)s(): %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: standard
stream: ext://sys.stdout
info_file_handler:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: standard
filename: log/info.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
error_file_handler:
class: logging.handlers.RotatingFileHandler
level: ERROR
formatter: error
filename: log/errors.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
debug_file_handler:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: standard
filename: log/debug.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
critical_file_handler:
class: logging.handlers.RotatingFileHandler
level: CRITICAL
formatter: standard
filename: log/critical.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
warn_file_handler:
class: logging.handlers.RotatingFileHandler
level: WARN
formatter: standard
filename: log/warn.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
rroot:
class: logging.StreamHandler
level: NOTSET
formatter: standard
stream: ext://sys.stdout
root:
level: NOTSET
handlers: [rroot]
propogate: yes
loggers:
DDEBUG:
level: DEBUG
handlers: [ info_file_handler, error_file_handler, critical_file_handler, debug_file_handler, warn_file_handler]
propogate: no
IINFO:
level: INFO
handlers: [info_file_handler, error_file_handler, critical_file_handler, debug_file_handler, warn_file_handler]
propogate: yes
def setup_logging(path='logging.yaml', default_level=logging.INFO):
if os.path.exists(path):
with open(path, 'rt') as f:
try:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
coloredlogs.install(level='DEBUG')
except Exception as e:
print(e)
logging.basicConfig(level=default_level)
coloredlogs.install(level=default_level)
else:
logging.basicConfig(level=default_level)
coloredlogs.install(level=default_level)
alog = logging.getLogger('DDEBUG')
alog.debug('debug message!')
alog.info('info message!')
alog.error('error message')
alog.critical('critical message')
alog.warning('warning message')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question