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

Format pretty and json log entries #236

Open
mousseq opened this issue May 26, 2023 · 2 comments
Open

Format pretty and json log entries #236

mousseq opened this issue May 26, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@mousseq
Copy link

mousseq commented May 26, 2023

I would like to customize the pretty format and the json format in ways not currently supported.

  1. I cannot create a pretty format that is json as I cannot obtain the message text. That is, in an ordinary log message (not one invoked with an Error object), the message text is not available for formatting. Consequently, I cannot create a log entry that contains the the message text in json format.
    For example:
const foptions = {
   "timestamp":"{{dateIsoStr}}",
   "level":"{{logLevelName}}",
   "location":"{{filePathWithLine}}",
   "message": "{{message}}"
};
const pretty_log = JSON.stringify( foptions );

const options = {
   name: "pretty",
   type: "pretty",
   prettyLogTemplate: pretty_log,
   stylePrettyLogs: false
};

const plogger = new Logger( options );
  1. I would like to produce json log entries that do not include the _meta element or include only selected properties of the _meta element. It would be helpful if I could specify a format for the json output that includes only the fields that I want. This could be achieved by specifying the fields that I want or specifying the fields that I don't want.
    For example:
const json_options = {
    "include": [ "name", "message", "stack", "date", "logLevelName" ],
    "stack include" : [ "fullFilePath", "method" ]
};

const json_log = JSON.stringify( json_options );

const options = {
	name: "json",
	type: "json",
	jsonLogTemplate: json_log
};
const jlogger = new Logger( options );
@mousseq mousseq added the enhancement New feature or request label May 26, 2023
@jjm340
Copy link

jjm340 commented Apr 4, 2024

There's a workaround for this, but it's ugly:
You can create a subclass:

class CustomLogger extends BaseLogger<LoggerPayload> {
  constructor(
    settings?: ISettingsParam<LoggerPayload>,
    logObj?: LoggerPayload,
  ) {
    super(settings, logObj, 5);
  }

  override log(logLevelId: number, logLevelName: string, ...args: unknown[]) {
    return {
      logLevelId,
      logLevelName,
      ...args,
    } as any;
  }

Return the payload as any and it will get rid of the meta fields and only return what you decide to return in this function.

@jjm340
Copy link

jjm340 commented Apr 4, 2024

Nevermind, this just breaks the logger!

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

No branches or pull requests

2 participants