tslog icon indicating copy to clipboard operation
tslog copied to clipboard

Format pretty and json log entries

Open mousseq opened this issue 2 years ago • 2 comments

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 avatar May 26 '23 15:05 mousseq

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 avatar Apr 04 '24 23:04 jjm340

Nevermind, this just breaks the logger!

jjm340 avatar Apr 04 '24 23:04 jjm340