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

Use pino logMethod hook to format audit logs #363

Open
nicola88 opened this issue Apr 23, 2024 · 5 comments · May be fixed by #366
Open

Use pino logMethod hook to format audit logs #363

nicola88 opened this issue Apr 23, 2024 · 5 comments · May be fixed by #366
Labels
enhancement New feature or request logging

Comments

@nicola88
Copy link

nicola88 commented Apr 23, 2024

Feature proposal

Use pino logMethod hook to enrich audit logs with basic fields, like version, timestamp and checksum.

The hook would filter the audit logs based on the level (greater than 1000) and wrap them into a standard field to ensure audit logs coming from different services follow the same data model.

Feature description

All services generating audit logs need to follow a common data model to ensure end users can aggregate and query them in a unified way.

We propose to use pino logMethod hook to intercept audit logs based on their log level, wrap the object passed as first argument to the log method and enrich it with some computed fields (version, timestamp, checksum, etc.).

Feature snippet example

The following snippet provides an example of how we imagined to configure pino to generate audit logs.

const { createHash } = require('node:crypto')
const pino = require('pino')

const options = {
  customLevels: {
    audit: 1100,
  },
  hooks: {
    logMethod(inputArgs, method, level) {
      if (level > 1000 && inputArgs.length >= 2) {
        const object = inputArgs.shift()
        const auditObject = {
          auditEvent: {
            version: '1.0.0',
            timestamp: new Date().toISOString(),
            checksum: {
              algorithm: 'sha512',
              value: createHash('sha512')
                .update(JSON.stringify(object))
                .digest('hex'),
            },
            metadata: object,
          },
        }
        return method.apply(this, [auditObject, ...inputArgs])
      }
      return method.apply(this, inputArgs)
    },
  },
}

const logger = pino(options)

const metadata = {
  event: 'AM/AppointmentCreated/v1',
  resource: 'AM/Appointment/appointment-12345',
  user: 'auth0|dr.john.doe',
  operation: 'CRUD/POST',
  source: 'appointment-manager',
}

logger.audit(metadata, 'event')
@nicola88 nicola88 changed the title Use pino logMethod hook to enrich audit logs Use pino logMethod hook to format audit logs Apr 23, 2024
@fredmaggiowski
Copy link
Member

Hi, as for #344 I'd not bind the library to the concept of custom logging methods (e.g. audit), however providing customized log hooks from your module in order to make you capable of doing what you are proposing makes completely sense to me, can you submit a pr?

@fredmaggiowski fredmaggiowski added logging enhancement New feature or request labels Apr 23, 2024
@nicola88
Copy link
Author

Thanks, we will open a PR soon to expose the custom hooks.

We were also wondering how we could avoid duplicating the hook implementation code across all projects generating audit logs, since it would become cumbersome to maintain over time and also quite bad in terms of developer experience, as Giulio pointed out (thinking about customers that want to use audit logs in their services).

Do you have any suggestion how we could avoid this? Maybe having in lc39 or the custom plugin lib a collection of ready-to-use hooks? Or a dedicated configuration option to enable audit trail, that will both add the custom log level and the hook? That would also make easier to filter the log not simply assuming their level to be greater than a thousand (level > 1000 && inputArgs.length >= 2), but checking that they match exactly the chosen log level (level === 1100 && ... from the snippet).

@fredmaggiowski
Copy link
Member

We were also wondering how we could avoid duplicating the hook implementation code across all projects generating audit logs, since it would become cumbersome to maintain over time and also quite bad in terms of developer experience, as Giulio pointed out (thinking about customers that want to use audit logs in their services).

I'd create a specific template for services that may need audit or a supporting lib that provides the log definitions to be supplied to lc39.

I'd not add custom logics to lc39 (and neither to the cplib)

@nicola88
Copy link
Author

Make sense, we can integrate our custom hook and also the related documentation in the Mia Care Node.js template. I close this issue and will open the PR in the next days.

@nicola88 nicola88 reopened this Apr 29, 2024
@nicola88 nicola88 linked a pull request Apr 29, 2024 that will close this issue
6 tasks
@nicola88
Copy link
Author

I opened the PR #366

@fredmaggiowski fredmaggiowski linked a pull request May 3, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request logging
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants