1.2.3 • Published 3 years ago

winston3-logstash-transport v1.2.3

Weekly downloads
993
License
MIT
Repository
github
Last release
3 years ago

winston3-logstash-transport

NPM

Actual version published on npm Master build Total npm module downloads Codacy Badge Codacy Coverate Badge Dependencies badge

A winston@3 transport for LogStash.

This winston transport has been rewritten from both winston-logastah and winston-logstash-udp to use the new Transport behavior from winston@3.

Where possible, this has been updated to mimic the behaviors of the original modules. There are some changes that have been made to allow the transport to handle either TCP or UDP connections to LogStash, instead of being dedicated to a single transport-layer protocol.

Usage

const Winston = require('winston');
const WinstonLogStash = require('winston3-logstash-transport');

const logger = Winston.createLogger();

logger.add(new WinstonLogStash({
  mode: 'udp',
  host: '127.0.0.1',
  port: 28777
}));

logger.error('Some Error');

API

constructor(options)

Create a new Logstash Transport

options

NameTypeDescriptionValid ValuesDefaultTCPUDP
modestringThe protocol to use to connect to LogStash. tcp is an alias for tcp4 and udp is an alias for udp4.udp udp4 udp6 tcp tcp4 tcp6'udp4'✔️✔️
localhoststringThe hostname sent to LogStashAnyos.hostname✔️✔️
hoststringThe LogStash server ip or hostnameAny valid IP or host address127.0.0.1 (ip4)::0 (ip6)✔️✔️
portintegerThe LogStash server port numberAny integer28777✔️✔️
applicationNamestringThe application name sent to LogStashAnyprocess.title✔️✔️
pidstringThe Operating System process ID for this processAny valid PIDprocess.pid✔️✔️
silentbooleanOffline/Silent mode enabledfalse✔️✔️
maxConnectRetriesintegerThe number of attempts to reconnect to make before erroring outAny integer4✔️✔️
timeoutConnectRetriesintegerThe number of milliseconds to wait between connection attemptsAny integer100✔️✔️
labelstringThe LogStash label to send with the informationAnyprocess.title✔️✔️
sslEnablebooleanWhether SSL/TLS connection should be attempted when connecting via TCPfalse✔️
sslKeyfilepathThe filepath to the SSL KeyAny valid filepath''✔️
sslCertfilepathThe filepath to the SSL CertAny valid filepath''✔️
sslCAfilepath or Array(filepaths)The filepath(s) to the Certificat Authority (CA) Intermediary CertsAny valid filepath(s)''✔️
sslPassPhrasestringThe SSL Cert PassPhrase (if any)Any''✔️
rejectUnauthorizedbooleanEnable connection rejection when cert is not validfalse✔️
trailingLineFeedbooleanEnable appending end of line character to UDP outputfalse✔️
trailingLineFeedCharstringThe type of end of line character(s) to append to UDP outputAnyos.EOL✔️
formattedbooleanEnable/Disable delivery of standard pre-formatted JSON payloads. See Message Payloads for more info.true✔️✔️

Message Payloads

By default or when options.formatted is explicitly set true, this module delivers a standard message payload to logstash as follows:

{
  "timestamp": new Date().toISOString(), // The time the payload was created
  "message": "",                         // JSON Stringified version of your message
  "level": "",                           // The logger level of the message
  "label": `${options.label}`,
  "application": `${options.applicationName}`,
  "serverName": `${options.localhost}`,
  "pid": `${options.pid}`
}

In this case when the log message is a string, boolean, or Number value, then the message is a stringified as:

{
  "data": `${message}`
}

If options.formatted is set to false, then the entire Winston log message object is JSON.stringified and then set to logstash.

Logstash Configuration

Having logstash ingest preformatted messages delivered by this module can be done with a configuration file similar to below:

input {
  # Sample input over TCP
  tcp {
    codec => json
    port => 28777
    add_field => { "category" => "winston_log" }
  }
}
filter {
  if [category] == "winston_log" {
    json {
      source => "message"
    }
    json {
      source => "data"
      remove_field => [ "[headers][secret]", "[headers][apikey]" ]
    }
  }
}
output {
  if [category] == "winston_log" {
    stdout {
      codec => json
    }
    elasticsearch {
      id => "winston_log_tcp"
      index => "winston_log-%{+YYYY.MM.dd}"
      hosts => ["192.168.1.1:9200"] # Use the address of your Elasticsearch server
    }
  }
}

License

Copyright (c) 2018, 2019 Jay Reardon

Copyright (c) 2019-2021 Out of Sync Studios LLC -- Licensed under the MIT license.