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

[mergingObject] under custom property #1838

Open
stychu opened this issue Oct 26, 2023 · 4 comments
Open

[mergingObject] under custom property #1838

stychu opened this issue Oct 26, 2023 · 4 comments

Comments

@stychu
Copy link

stychu commented Oct 26, 2023

Hello,
Im struggling a bit with how I could nest my logs under the custom property in the log.
For example logContext as custom prop and a log with [mergingObject] as log.info({some: 'asd'}, 'abc')
I would Like it to be logged as
{pid: 1, hostname: 'asdsd', msg: 'abc', logContext: {some: 'asd'}}
How I can do that?
I've tried with formatters.log option as

  formatters: {
    log: (object) => {
      return { logContext: object }
    },
  }

But it breaks logging with errors (errors are swallowed).

@mcollina
Copy link
Member

Can you include a full example showing errors that are swallowed?

@stychu
Copy link
Author

stychu commented Oct 26, 2023

@mcollina

// logger
const loggerInstance = pino({
  level: env.LOGGER_LOG_LEVEL,
  timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
  errorKey: 'error',
  formatters: {
    level: (label) => {
      return { level: label.toUpperCase() };
    },
     log: (object) => {
      return { logContext: object };
    },
  },
  redact: ['req.headers', 'res.headers'],
});

const logger = loggerInstance.child(
  { kind: 'application' },
  { msgPrefix: '[APP] ' },
);

export const getLogger = (subKind: string) =>  logger.child({ subKind: subKind });

export const httpLogger = pinoHttp({
  logger: loggerInstance.child({ kind: 'access' }),
  msgPrefix: '[HTTP] ',
});

export const serverLogger = getLogger('server');
export const oidcLogger = getLogger('oidc');
export const trpcLogger = getLogger('trpc');

and for example doing

serverLogger.error(new Error('first error'), 'first err');
serverLogger.error({ error: new Error('second error') }, 'second err');

---
[19:36:20.085] ERROR (51006): [APP] first err
    kind: "application"
    subKind: "server"
    logContext: {
      "error": {}
    }
[19:36:20.086] ERROR (51006): [APP] second err
    kind: "application"
    subKind: "server"
    logContext: {
      "error": {}
    }

@stychu
Copy link
Author

stychu commented Nov 3, 2023

@mcollina any suggestion how can I put all logs under someprop ??

@bngarren
Copy link
Contributor

@stychu Take a look at nestedKey option.

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

3 participants