05-19-2020 04:25 PM
I need help with using python 3's standard logging module. I can't figure it out. Any clue is appreciatd!
It broke at the logging.config.dictConfig(config) line.
When I run main_log.py, I got this error message:
logging.config.dictConfig(config)
File "C:\Python38\lib\logging\config.py", line 808, in dictConfig
dictConfigClass(config).configure()
File "C:\Python38\lib\logging\config.py", line 570, in configure
raise ValueError('Unable to configure handler '
ValueError: Unable to configure handler 'file'
===========
main_log.py
===========
import logging
import logging.config
import os
import yaml
class SetupLogging:
def __init__(self):
self.default_config = os.path.join(os.path.dirname(
os.path.abspath('__file__')), "templates/logging.yaml")
def setup_logging(self, default_level=logging.INFO, env_key='LOG_CFG'):
path = self.default_config
env_path = os.getenv(env_key, None)
if env_path:
path = env_path
if os.path.exists(path):
config = yaml.safe_load(open(path, 'rt'))
logging.config.dictConfig(config)
else:
print('Error in loading logging configuration. Using default configs')
logging.basicConfig(level=default_level)
SetupLogging().setup_logging()
logging.info('%s', 'test.')
===========
logging.yaml
===========
---
version: 1
disable_existing_loggers: False
formatters:
user:
format: "[%(asctime)s][%(levelname)s] %(message)s"
datefmt: "%H:%M:%S"
simple:
format: "[%(asctime)s][%(name)s][%(levelname)s] %(message)s"
datefmt: "%m/%d/%Y %H:%M:%S"
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: user
stream: ext://sys.stdout
file:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: simple
filename: /opt/abc/file.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
loggers:
xyz_tool:
level: INFO
handlers: [console, file]
propagate: no
root:
level: INFO
handlers: [file, ]
...
Solved! Go to Solution.
05-21-2020 03:47 PM - edited 05-21-2020 03:48 PM
Hi
I have re-formatted yaml .. https://onlineyamltools.com/prettify-yaml
just to make it look pretty :)
Make sure /opt/abc/ directory exists to create the log file file.log.
I used the Desktop for test.
version: 1
disable_existing_loggers: false
formatters:
user:
format: '[%(asctime)s][%(levelname)s] %(message)s'
datefmt: '%H:%M:%S'
simple:
format: '[%(asctime)s][%(name)s][%(levelname)s] %(message)s'
datefmt: '%m/%d/%Y %H:%M:%S'
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: user
stream: 'ext://sys.stdout'
file:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: simple
filename: /Users/omz/Desktop/file.log
maxBytes: 10485760
backupCount: 20
encoding: utf8
loggers:
xyz_tool:
level: INFO
handlers:
- console
- file
propagate: 'no'
root:
level: INFO
handlers:
- fileOnly changed the file open method in the python code.
import logging
import logging.config
import os
import yaml
class SetupLogging:
def __init__(self):
self.default_config = os.path.join(os.path.dirname(
os.path.abspath('__file__')), "logging.yaml")
def setup_logging(self, default_level=logging.INFO, env_key='LOG_CFG'):
path = self.default_config
env_path = os.getenv(env_key, None)
if env_path:
path = env_path
if os.path.exists(path):
with open(path, 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
else:
print('Error in loading logging configuration. Using default configs')
logging.basicConfig(level=default_level)
SetupLogging().setup_logging()
logging.info('%s', 'test.')05-21-2020 03:47 PM - edited 05-21-2020 03:48 PM
Hi
I have re-formatted yaml .. https://onlineyamltools.com/prettify-yaml
just to make it look pretty :)
Make sure /opt/abc/ directory exists to create the log file file.log.
I used the Desktop for test.
version: 1
disable_existing_loggers: false
formatters:
user:
format: '[%(asctime)s][%(levelname)s] %(message)s'
datefmt: '%H:%M:%S'
simple:
format: '[%(asctime)s][%(name)s][%(levelname)s] %(message)s'
datefmt: '%m/%d/%Y %H:%M:%S'
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: user
stream: 'ext://sys.stdout'
file:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: simple
filename: /Users/omz/Desktop/file.log
maxBytes: 10485760
backupCount: 20
encoding: utf8
loggers:
xyz_tool:
level: INFO
handlers:
- console
- file
propagate: 'no'
root:
level: INFO
handlers:
- fileOnly changed the file open method in the python code.
import logging
import logging.config
import os
import yaml
class SetupLogging:
def __init__(self):
self.default_config = os.path.join(os.path.dirname(
os.path.abspath('__file__')), "logging.yaml")
def setup_logging(self, default_level=logging.INFO, env_key='LOG_CFG'):
path = self.default_config
env_path = os.getenv(env_key, None)
if env_path:
path = env_path
if os.path.exists(path):
with open(path, 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
else:
print('Error in loading logging configuration. Using default configs')
logging.basicConfig(level=default_level)
SetupLogging().setup_logging()
logging.info('%s', 'test.')05-25-2020 08:54 PM
Thank you omz! That gave me a great start. I not only was able to run it and but also expanded to more handlers to multiple files, the way I want.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide