pinojs / pino-socket

🌲 A transport for sending pino logs to network sockets
43 stars 14 forks source link

Unix socket example #98

Open tytremblay opened 1 year ago

tytremblay commented 1 year ago

The Issue

I'm trying to use pino-socket as a transport to log to syslog via /dev/log on my Ubuntu 22.04 system using node 18.

import pino from 'pino';

const logger = pino({
  level: 'info',
  transport: {
    targets: [
      {
        level: 'trace',
        target: 'pino-socket',
        options: {
          unixsocket: '/dev/log',
        },
      },
      { level: 'trace', target: 'pino-pretty', options: {} },
    ],
  },
});

logger.info(`pino logger instantiated `);

export { logger };

Here's the output I see in my console:

[15:46:00.992] INFO (501708): pino logger instantiated 
Error: send ECONNREFUSED
    at doSend (node:dgram:716:16)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
    at afterDns (node:dgram:662:5)
    at Socket.send (node:dgram:672:5)
    at Writable.write [as _write] (/home/ty/Documents/rtr/rtr-webapps/node_modules/pino-socket/lib/UdpConnection.js:28:16)
    at doWrite (node:internal/streams/writable:411:12)
    at clearBuffer (node:internal/streams/writable:572:7)
    at onwrite (node:internal/streams/writable:464:7)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

And of course no logs show up when I run tail -f /var/log/syslog | grep pino

winston-syslog seems to work

But I would much rather use pino

import { config, createLogger, transports } from 'winston';
import { Syslog, SyslogTransportOptions } from 'winston-syslog';

const opt: SyslogTransportOptions = {
  protocol: 'unix',
  path: '/dev/log',
  facility: 'user',
  localhost: '',
  app_name: 'my-great-app',
};

const Logger = createLogger({
  levels: config.syslog.levels,
  transports: [new transports.Console()],
});
const sysLogTransport = new Syslog(opt);
Logger.add(sysLogTransport);

Logger.info(`Logger instantiated `);
export { Logger };

I get this line in /var/log/syslog :

May 10 15:59:32 my-ubuntu-4 my-great-app[504122]: {"level":"info","message":"Logger instantiated "}

The Ask

I'm sure I'm doing something wrong. Could you please provide an example?

mcollina commented 1 year ago

I think the following should work:

import pino from 'pino';

const logger = pino({
  level: 'info',
  transport: {
    targets: [
      {
        level: 'trace',
        target: 'pino-socket',
        options: {
          unixsocket: '/dev/log',
          mode: 'tcp',
        },
      },
      { level: 'trace', target: 'pino-pretty', options: {} },
    ],
  },
});

logger.info(`pino logger instantiated `);

export { logger };

Note that this shows a bug here, as 'tcp' should likely be set if unixsocket is passed through.

mcollina commented 1 year ago

@tytremblay would you like to send a PR with the fix?

tytremblay commented 1 year ago

I'll give it a shot. In the meantime, I found this article that suggest my application should log to stdout or stderr and let the system its running on pipe that output to syslog if they so desire. My sysadmin agrees, so we've gone with that approach for now.

Logs are a stream, and it behooves everyone to treat them as such. Your programs should log to stdout and/or stderr and omit any attempt to handle log paths, log rotation, or sending logs over the syslog protocol. Directing where the program’s log stream goes can be left up to the runtime container: a local terminal or IDE (in development environments), an Upstart / Systemd launch script (in traditional hosting environments), or a system like Logplex/Heroku (in a platform environment).

jsumners commented 1 year ago

I'll give it a shot. In the meantime, I found this article that suggest my application should log to stdout or stderr and let the system its running on pipe that output to syslog if they so desire. My sysadmin agrees, so we've gone with that approach for now.

Yes, that is the Pino way. See also https://www.nearform.com/blog/pino-the-fastest-node-js-logger-for-production/ (https://www.npmjs.com/package/@sematext/logagent).