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

How to close/flush/end ? #2

Closed
sp00x opened this issue Jul 5, 2024 · 3 comments
Closed

How to close/flush/end ? #2

sp00x opened this issue Jul 5, 2024 · 3 comments
Assignees

Comments

@sp00x
Copy link

sp00x commented Jul 5, 2024

Was just testing this briefly and I notice that the scripts does not automatically close on completion, meaning there's probably some event handler or something that is left hanging, which is different from when using 'pino/file' where it terminates properly.

Is there some close() or end() or something method that needs to be called to make it terminate?

import pino from 'pino';

const transport = pino.transport({
  targets: [
    {
      level: 'trace',
      'target': '@jvddavid/pino-rotating-file',
      options: {
        path: "logs",
        //pattern: "log-%Y-%M-%d-%H-%m-%s-%l-%N.log",
        pattern: "log-%Y-%M-%d-%N.log",
        maxSize: 1024 * 1024 * 10,
        sync: true,
        fsync: true,
        append: true,
        mkdir: true,
      }
    }, 
    // {
    //   level: 'trace',
    //   target: 'pino/file',
    //   options: {
    //     destination: 'test.log'
    //   }
    // },
    {
      level: 'debug',
      target: 'pino-pretty',
      options: {
      }
    }
  ]
});

let logger = pino.pino(transport);

logger.info("hello");

// script is kept open, does not terminate .. terminates properly with pino/file
@1340145680
Copy link

Same problem, in my express program I manually restart serve will prompt me the port is occupied, after troubleshooting is due to the pino-rotating-file plugin is not properly closed, in the project compressed into a zip will also be prompted the log file is being used!

@1340145680
Copy link

I found the problem, the reason is that SIGINT “does not emit the ‘exit’ event and pino transport cannot call the close method correctly.
pinojs/pino#2002 (comment)

@jvddavid jvddavid self-assigned this Dec 12, 2024
@jvddavid
Copy link
Owner

jvddavid commented Dec 12, 2024

Thanks for the feedback, I used the close-with-grace package to perform the stream closure.

import closeWithGrace from 'close-with-grace'
import build from 'pino-abstract-transport'
import { RotateFileStream, type RotateFileTransportOptions } from './RotateFileStream'

export default (opts: RotateFileTransportOptions) => {
  const destination = new RotateFileStream(opts)
  closeWithGrace(() => {
    destination.end()
  })
  return build(
    source => {
      source.pipe(destination)
    },
    {
      close(err, cb) {
        destination.on('close', cb.bind(null, err))
        destination.end()
      },
      parse: 'lines',
      parseLine: line => line
    }
  )
}

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