Socket
Socket
Sign inDemoInstall

pino-socket

Package Overview
Dependencies
15
Maintainers
4
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pino-socket

A pino 'transport' for writing to a tcp, udp, or unix socket


Version published
Weekly downloads
5.9K
increased by6.34%
Maintainers
4
Install size
388 kB
Created
Weekly downloads
 

Readme

Source

pino-socket

npm version Build Status js-standard-style

Lead maintainer: jsumners

This module provides a "transport" for pino that simply forwards messages to an arbitrary socket. The socket can be UDPv4 or TCPv4. The module can echo the received logs or work silently.

You should install pino-socket globally for ease of use:

$ npm install --production -g pino-socket

Usage as Pino Transport

You can use this module as a pino transport like so:

const pino = require('pino')
const transport = pino.transport({
  target: 'pino-socket',
  options: {
    address: '10.10.10.5',
    port: 5000,
    mode: 'tcp'
  }
})
pino(transport)

Options

NameDescription
addressThe host address to connect to. Default: 127.0.0.1.
portThe host port to connect to. Default: 514.
unixsocketThe unix socket path for the destination. Default: ​.
modeEither tcp or udp. Default: udp.
secureEnable secure (TLS) connection. Default: false.
noverifyAllow connection to server with self-signed certificates. Default: false.
reconnectEnable reconnecting to dropped TCP destinations. Default: false.
reconnectTriesNumber of times to attempt reconnection before giving up. Default: Infinity.
backoffStrategyThe backoff strategy to use on TCP destinations. The backoff strategy must implement the BackoffStrategy interface. lternatively, you can configure the backoff strategy using primitive data (see below). Default: new FibonacciStrategy().
backoffStrategy.nameThe backoff strategy name, either exponential or fibonacci. Default: fibonacci.
backoffStrategy.randomisationFactorThe backoff randomisation factor, must be between 0 and 1. Default: 0.
backoffStrategy.initialDelayThe backoff initial delay in milliseconds. Default: 100.
backoffStrategy.maxDelayThe backoff maximum delay in milliseconds. Default: 10000.
backoffStrategy.factorThe exponential backoff factor, must be greater than 1. Default: 2.
recoveryEnable a recovery mode when the TCP connection is lost which store data in a memory queue (FIFO) until the queue max size is reached or the TCP connection is restored. Default: false.
recoveryQueueMaxSizeThe maximum size of items added to the queue. When reached, oldest items "First In" will be evicted to stay below this size. Default: 1024.
recoveryQueueSizeCalculationFunction used to calculate the size of stored items. The item is passed as the first argument and contains a data (Buffer) and encoding (String) attribute. Default: (item) => item.data.length + item.encoding.length.
onBeforeDataWriteFunction used to manipulate TCP data before being written to the socket. Operations preformed here must be synchronous. Format: (data) => Buffer. Default: null

Events

NameCallback SignatureDescription
open(address: AddressInfo) => voidEmitted when the TCP or UDP connection is established.
socketError(error: Error) => voidEmitted when an error occurs on the TCP or UDP socket. The socket won't be closed.
socketClose(error: Error|null) => voidEmitted after the TCP socket is closed. The argument error is defined if the socket was closed due to a transmission error.
close(hadError: Boolean) => voidEmitted after the TCP or UDP socket is closed and won't reconnect. The argument hadError is a boolean which says if the socket was closed due to a transmission error.
reconnectFailure(error: Error|undefined) => voidEmitted when the maximum number of backoffs (i.e., reconnect tries) is reached on a TCP connection.

IMPORTANT: In version prior to 6.0, an error event was emitted on the writable stream when an error occurs on the TCP or UDP socket. In other words, it was not possible to write data to the writable stream after an error occurs on the TCP or UDP socket. If you want to restore the previous behavior you can do:

transport.on('socketError', () => {
  transport.end()
})

Alternatively, you can propagate the socket error using:

transport.on('socketError', (err) => {
  transport.emit('error', err)
})

In this case, make sure that you are listening to the error event otherwise you will get an Uncaught Error.

Usage as Pino Legacy Transport

Pino supports a legacy transport interface that is still supported by this module. Given an application foo that logs via pino, and a system that collects logs on port UDP 5000 on IP 10.10.10.5, you would use pino-socket like so:

$ node foo | pino-socket -a 10.10.10.5 -p 5000

OR

$ node foo | pino-socket -u /tmp/unix.sock

CLI Options

  • --settings (-s): read settings from a JSON file (switches take precedence).
  • --unixsocket (-u): the unix socket path for the destination. Default: ​.
  • --address (-a): the address for the destination socket. Default: 127.0.0.1.
  • --port (-p): the port for the destination socket. Default: 514.
  • --mode (-m): either tcp or udp. Default: udp.
  • --secure (-tls): enable secure (TLS) connection for TCP (only works with --mode=tcp).
  • --noverify (-nv): allow connection to server with self-signed certificates (only works with --secure).
  • --reconnect (-r): enable reconnecting to dropped TCP destinations. Default: off.
  • --reconnectTries <n> (-t <n>): set number (<n>) of reconnect attempts before giving up. Default: infinite.
  • --echo (-e): echo the received messages to stdout. Default: enabled.
  • --no-echo (-ne): disable echoing received messages to stdout.
  • --recovery: enable recovery mode for TCP (only works with --mode=tcp). Default: off.
  • --recovery-queue-max-size <n>: maximum size of items (<n>) added to the recovery queue. Default: 1024.
  • --max-udp-packet-size: maximum size of udp packet; Default: unlimited.
Settings JSON File

The --settings switch can be used to specify a JSON file that contains a hash of settings for the application. A full settings file is:

{
  "address": "127.0.0.1",
  "port": 514,
  "mode": "tcp",
  "secure": false,
  "noverify": false,
  "reconnect": true,
  "reconnectTries": 20,
  "echo": false
}

Note that command line switches take precedence over settings in a settings file. For example, given the settings file:

{
  "address": "10.0.0.5",
  "port": 514
}

And the command line:

$ yes | pino-socket -s ./settings.json -p 1514

The connection will be made to address 10.0.0.5 on UDP port 1514.

License

MIT License

Keywords

FAQs

Last updated on 17 Oct 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc