Skip to content
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

Get colorized label in messageFormat string #532

Open
TheSecurityDev opened this issue Sep 21, 2024 · 1 comment
Open

Get colorized label in messageFormat string #532

TheSecurityDev opened this issue Sep 21, 2024 · 1 comment

Comments

@TheSecurityDev
Copy link

TheSecurityDev commented Sep 21, 2024

I want to format the log messages like this (colored level label surrounded by brackets):

[22:37:36.893] [INFO]: Some log message

Previously I was doing it by specifying a customPrettifiers property like this:

customPrettifiers: {
     level: (_inputData, _key, _log, { labelColorized }) => `[${labelColorized}]`, // Add brackets around level label
}

However, this no longer works for me because now I have multiple transports and I get errors about the function in the config not being serializable.

So my next idea was to use the messageFormat string, like this:

messageFormat: `[{levelLabel}]: {msg}`

As well as adding the "level" property to the ignored fields, so we don't get both of them.

This works, but the level isn't colored.

Now I figured I can just do this in production, where I don't color the levels anyway. I really want the format to stay the same as my old logs in production, but it's not a big deal in development (where I want the colored labels).

However, there's still a small issue. I can't get it to remove the colon after the timestamp. Previously it looked like: [22:37:36.893] [INFO]: Some log message, but specifying to remove the level makes it look like [22:37:36.893]: Some log message, so when I add the level in the messageFormat string it ends up looking like this:

[22:37:36.893]: [INFO]: Some log message

And I can't figure out how to remove the extra colon after the timestamp. It's really not a huge deal, but I want to keep the format the same as my old logs.

I tried removing all fields and adding the timestamp in the message format string like {time} {levelLabel}: {msg}, but then the issue is that it doesn't format the timestamp, it just outputs it as a number. That's another issue I should probably open separately, since the {time} value in the messageFormat string should probably respect the translateTime property.

Context:

The main thing is all my previous logs have the level like [ERROR]: or [INFO]:, and I have my Papertrail searches and alerts set up to observe this format, otherwise it wouldn't be a big deal, and I would just leave the default level format alone.

And yes, I am trying to implement a JSON-based log system now so it shouldn't matter what the log format is. That's why I need the second transport now, since I'm implementing Better Stack (Logtail). But I want to log to both, because just logging JSON encoded string to STDOUT doesn't work well on Heroku, since Heroku's Logplex logger puts the whole message into the message key, and so the level key is put in the wrong place (Better Stack expects it to be the root field). And I still have alerts and stuff in Papertrail (where the Heroku logs are sent) that I want to keep.

If anyone has any suggestions for this whole mess, please let me know. It's been a massive pain trying to get this all to work. I even had to create a custom Logtail transport since the @logtail/pino one wouldn't handle custom level labels.

@jsumners
Copy link
Member

You should write your own "prettifier". This module is not intended to be a generic tool for reformatting JSON logs into any other format. It's purpose is to format JSON logs into a human readable format, with some limited customization of that format available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants