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

Unix socket example #98

Open
tytremblay opened this issue May 10, 2023 · 4 comments
Open

Unix socket example #98

tytremblay opened this issue May 10, 2023 · 4 comments

Comments

@tytremblay
Copy link

tytremblay commented May 10, 2023

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
Copy link
Member

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
Copy link
Member

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

@tytremblay
Copy link
Author

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
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants