colin-b / logging_json Goto Github PK
View Code? Open in Web Editor NEWJSON formatter for python logging
License: MIT License
JSON formatter for python logging
License: MIT License
Hi,
I have been testing out logging_json over the last couple of days.
I am able to generate JSON logs.
I would like to add some custom fields so that they are automatically included in the JSON log without the user having to provide additional information. The value of those fields are either queried via environment variable or in-house modules that have functions to retrieve those information.
Some of those fields are
"airport" - value is a string
"building" - value is an integer
"linear_dimension" - value is an unsigned integer
"weight" - value is a floating point number
Cheers
Do we need to log from an except context, does it work in a finally ? Outside?
python version is 3.11.5
my logger setting is here
logger = logging.getLogger(type(self).__name__)
# set Log Level
logger.setLevel(logging.INFO)
# set Stream Handle
ch = logging.StreamHandler()
ch.encoding = 'utf-8'
ch.setLevel(logging.INFO)
stream_formatter = logging_json.JSONFormatter(fields={
"name": "name",
"level_name": "levelname",
"thread": "thread",
"process": "process",
"response": "response"
})
ch.setFormatter(stream_formatter)
logger.addHandler(ch)
# set Formatter
file_formatter = logging_json.JSONFormatter(fields={
"name": "name",
"level_name": "levelname",
"thread": "thread",
"thread_name": "threadName",
"process": "process",
"process_name": "processName"
})
# set File Handler
fh = logging.FileHandler(filename=f"{os.getcwd()}/logs/dummy.log",encoding='utf-8')
fh.setFormatter(file_formatter)
fh.setLevel(logging.INFO)
logger.addHandler(fh)
return logger
logging.info("테스트")
result is below
{"name": "BaseLogger", "level_name": "INFO", "thread": 139637689555784, "thread_name": "MainThread", "process": 314, "process_name": "SpawnProcess-32", "message": "\uc548\ub155"}
encoding utf-8 does not work
Hi,
i discovered an issue that prints strings with unicode characters as in this example "\u0451".
To fix it I suggest to add parameter ensure_ascii=False
in json.dumps() call at line 99 in logging_json._formatter.py
I hope it will help!
Executing dict configuratio example yields following exception:
Traceback (most recent call last):
File "/usr/local/Cellar/[email protected]/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/config.py", line 543, in configure
formatters[name] = self.configure_formatter(
File "/usr/local/Cellar/[email protected]/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/config.py", line 652, in configure_formatter
if '()' in config:
TypeError: argument of type 'JSONFormatter' is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/matus/test2.py", line 13, in <module>
logging.config.dictConfig({
File "/usr/local/Cellar/[email protected]/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/config.py", line 809, in dictConfig
dictConfigClass(config).configure()
File "/usr/local/Cellar/[email protected]/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/config.py", line 546, in configure
raise ValueError('Unable to configure '
ValueError: Unable to configure formatter 'json'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.