-
Notifications
You must be signed in to change notification settings - Fork 715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept a Callable for the serialize argument when adding a sink #278
Comments
Hi @gjeusel, glad you like Accepting a callable to The thing is that there is already two ways to customize the serialized output: use a custom For example, you can convert your def _serialize_record_elk(text):
record = text.record
...
print(orjson.dumps(serializable, default=str).decode("utf-8"))
logger.add(_serialize_record_elk, serialize=False, format="{message}") If you need to send structured logs to a file (or any kind of sink in general), a similar trick can be accomplished by using a custom def _serialize_record_elk(record):
...
return orjson.dumps(serializable, default=str).decode("utf-8") + "\n"
logger.add("file.log", serialize=False, format=_serialize_record_elk) # Edit: above snippet was incorrect
def _serialize_record_elk(record):
...
record["extra"]["serialized"] = orjson.dumps(serializable, default=str).decode("utf-8")
return "{extra[serialized]}\n"
logger.add("file.log", serialize=False, format=_serialize_record_elk) Finally, if there is just a few attributes that you would like to be added to the logged message, an alternative is to use def patcher(record):
timestamp = record["time"].astimezone(pytz.utc)
timestamp = timestamp.strftime("%Y-%m-%dT%H:%M:%S.{:03d}Z").format(
timestamp.microsecond // 1000
)
record["@timestamp"] = timestamp
logger.configure(patcher=patcher)
logger.add(sys.stdout, serialize=True, format="{message}") I prefer that the |
Ahah perfect ! Thx ! |
No problem. 👍 I will add a recipe about the possible alternatives in the documentation. |
Hello, me again! import sys
import orjson
from loguru import logger
def _serialize_record_elk(record):
serializable = {
"log": {"level": record["level"].name}
}
return orjson.dumps(serializable, default=str).decode("utf-8") + "\n"
logger.add(sys.stdout, colorize=False, serialize=False, format=_serialize_record_elk)
logger.info("YO")
It seems it still tries to colorize the jsonified record, even asking explicitly not to ? |
@gjeusel Oh yeah, sorry, I'm dumb. >.< The Here is what I mean: # OK
logger.add(sys.stderr, format="{level} {message}")
# OK
logger.add(sys.stderr, format=lambda _: "{level} {message}\n")
# NOK
logger.add(sys.stderr, format=lambda record: "{} {}\n".format(record["level"], record["message"]) The string returned by the It's a bit counter-intuitive (hence my mistake), but it's necessary to allow the use of colors dynamically. The "idiomatic" workaround is to add the generated message to the def _serialize_record_elk(record):
...
record["extra"]["serialized"] = orjson.dumps(serializable, default=str).decode("utf-8")
return "{extra[serialized]}\n"
logger.add(sys.stdout, colorize=False, serialize=False, format=_serialize_record_elk) Again, it's also possible to |
I added a small recipe to the documentation: Serializing log messages using a custom function. |
Hello Delgan ! Love your library !
I was wondering what you would think about accepting a Callable for the
serialize
parameter inlogger.add
method ?This would ease customization of the jsonified records. And I'm thinking about ELK stack usage for example.
I managed to achieve this purpose by overwriting the
Handler._serialize_record
staticmethod:Still, that would be a nice to have in official doc IMO ! 😉
The text was updated successfully, but these errors were encountered: