Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A structured logger for Fluentd (Node.js)

License

NotificationsYou must be signed in to change notification settings

fluent/fluent-logger-node

Repository files navigation

fluent-logger implementation for Node.js inspired byfluent-logger-python.

NPM

Build Status

Install

$ npm install fluent-logger

Prerequistes

Fluent daemon should listen on TCP port.

Simple configuration is following:

<source>  @type forward  port24224</source><match **.*>  @type stdout</match>

Usage

Send an event record to Fluentd

Singleton style

varlogger=require('fluent-logger')// The 2nd argument can be omitted. Here is a default value for options.logger.configure('tag_prefix',{host:'localhost',port:24224,timeout:3.0,reconnectInterval:600000// 10 minutes});// send an event record with 'tag.label'logger.emit('label',{record:'this is a log'});

Instance style

varlogger=require('fluent-logger').createFluentSender('tag_prefix',{host:'localhost',port:24224,timeout:3.0,reconnectInterval:600000// 10 minutes});

The emit method has following signature

.emit([labelstring],<recordobject>, [timestamp number/date], [callback function])

Where only therecord argument is required. If the label is set it will beappended to the configured tag.

Disable automatic reconnect

Both Singleton and Instance style can disable automatic reconnect allowing the user to handle reconnect himself

logger.configure('tag_prefix',{host:'localhost',port:24224,timeout:3.0,enableReconnect:false// defaults to true});

Shared key authentication

Logger configuration:

varlogger=require('fluent-logger').createFluentSender('dummy',{host:'localhost',port:24224,timeout:3.0,reconnectInterval:600000,// 10 minutessecurity:{clientHostname:"client.localdomain",sharedKey:"secure_communication_is_awesome"}});logger.emit('debug',{message:'This is a message'});

Server configuration:

<source>  @type forward  port24224  <security>    self_hostname input.testing.local    shared_key secure_communication_is_awesome  </security></source><match dummy.*>  @type stdout</match>

See alsoFluentd examples.

TLS/SSL encryption

Logger configuration:

varlogger=require('fluent-logger').createFluentSender('dummy',{host:'localhost',port:24224,timeout:3.0,reconnectInterval:600000,// 10 minutessecurity:{clientHostname:"client.localdomain",sharedKey:"secure_communication_is_awesome"},tls:true,tlsOptions:{ca:fs.readFileSync('/path/to/ca_cert.pem')}});logger.emit('debug',{message:'This is a message'});

Server configuration:

<source>  @type forward  port24224  <transport tls>    ca_cert_path /path/to/ca_cert.pem    ca_private_key_path /path/to/ca_key.pem    ca_private_key_passphrase very_secret_passphrase  </transport>  <security>    self_hostname input.testing.local    shared_key secure_communication_is_awesome  </security></source><match dummy.*>  @type stdout</match>

FYI: You can generate certificates using fluent-ca-generate command since Fluentd 1.1.0.

See alsoHow to enable TLS/SSL encryption.

Mutual TLS Authentication

Logger configuration:

varlogger=require('fluent-logger').createFluentSender('dummy',{host:'localhost',port:24224,timeout:3.0,reconnectInterval:600000,// 10 minutessecurity:{clientHostname:"client.localdomain",sharedKey:"secure_communication_is_awesome"},tls:true,tlsOptions:{ca:fs.readFileSync('/path/to/ca_cert.pem'),cert:fs.readFileSync('/path/to/client-cert.pem'),key:fs.readFileSync('/path/to/client-key.pem'),passphrase:'very-secret'}});logger.emit('debug',{message:'This is a message'});

Server configuration:

<source>  @type forward  port24224  <transport tls>    ca_path /path/to/ca-cert.pem    cert_path /path/to/server-cert.pem    private_key_path /path/to/server-key.pem    private_key_passphrase very_secret_passphrase    client_cert_auth true  </transport>  <security>    self_hostname input.testing.local    shared_key secure_communication_is_awesome  </security></source><match dummy.*>  @type stdout</match>

EventTime support

We can also specifyEventTime as timestamp.

varFluentLogger=require('fluent-logger');varEventTime=FluentLogger.EventTime;varlogger=FluentLogger.createFluentSender('tag_prefix',{var eventTime=newEventTime(1489547207,745003500);// 2017-03-15 12:06:47 +0900logger.emit('tag',{message:'This is a message'},eventTime);

Events

varlogger=require('fluent-logger').createFluentSender('tag_prefix',{host:'localhost',port:24224,timeout:3.0,reconnectInterval:600000// 10 minutes});logger.on('error',(error)=>{console.log(error);});logger.on('connect',()=>{console.log('connected!');});

Logging Library Support

log4js

Uselog4js-fluent-appender.

winston

Before usingwinston support, you should install it IN YOUR APPLICATION.

varwinston=require('winston');varconfig={host:'localhost',port:24224,timeout:3.0,requireAckResponse:true// Add this option to wait response from Fluentd certainly};varfluentTransport=require('fluent-logger').support.winstonTransport();varfluent=newfluentTransport('mytag',config);varlogger=winston.createLogger({transports:[fluent,new(winston.transports.Console)()]});logger.on('flush',()=>{console.log("flush");})logger.on('finish',()=>{console.log("finish");fluent.sender.end("end",{},()=>{})});logger.log('info','this log record is sent to fluent daemon');logger.info('this log record is sent to fluent daemon');logger.info('end of log message');logger.end();

NOTE If you usewinston@2, you can usefluent-logger@2.7.0 or earlier. If you usewinston@3, you can usefluent-logger@2.8 or later.

stream

Several libraries use stream as output.

'use strict';constConsole=require('console').Console;varsender=require('fluent-logger').createFluentSender('tag_prefix',{host:'localhost',port:24224,timeout:3.0,reconnectInterval:600000// 10 minutes});varlogger=newConsole(sender.toStream('stdout'),sender.toStream('stderr'));logger.log('this log record is sent to fluent daemon');setTimeout(()=>sender.end(),5000);

Options

tag_prefix

The tag prefix string.You can specifynull when you useFluentSender directly.In this case, you must specifylabel when you callemit.

host

The hostname. Default value = 'localhost'.

Seesocket.connect

port

The port to listen to. Default value = 24224.

Seesocket.connect

path

The path to your Unix Domain Socket.If you setpath then fluent-logger ignoreshost andport.

Seesocket.connect

timeout

Set the socket to timetout aftertimeout milliseconds of inactivityon the socket.

Seesocket.setTimeout

reconnectInterval

Set the reconnect interval in milliseconds.If error occurs then reconnect after this interval.

requireAckResponse

Change the protocol to at-least-once. The logger waits the ack from destination.

ackResponseTimeout

This option is used when requireAckResponse is true. The default is 190. This default value is based on populartcp_syn_retries.

eventMode

SetEvent Modes. This logger supportsMessage,PackedForward andCompressedPackedForward.Default isMessage.

NOTE: We will change default toPackedForward and dropMessage in next major release.

flushInterval

Set flush interval in milliseconds. This option has no effect inMessage mode.The logger stores emitted events in buffer and flush events for each interval.Default100.

messageQueueSizeLimit

Maximum number of messages that can be in queue at the same time. If a new message is received and it overflows the queue then the oldest message will be removed before adding the new item. This option has effect only inMessage mode. No limit by default.

security.clientHostname

Set hostname of this logger. Use this value for hostname based authentication.

security.sharedKey

Shared key between client and server.

security.username

Set username for user based authentication. Default values is empty string.

security.password

Set password for user based authentication. Default values is empty string.

sendQueueSizeLimit

Queue size limit in bytes. This option has no effect inMessage mode. Default is8 MiB.

tls

Enable TLS for socket.

tlsOptions

Options to pass to tls.connect when tls is true.

For more details, see following documents

internalLogger

Set internal logger object for FluentLogger. Useconsole by default.This logger requiresinfo anderror method.

Examples

Winston Integration

An example of integrating with Winston can be found at./example/winston.

You will need Docker Compose to run it. After navigating to./example/winston, rundocker-compose up and thennode index.js. You should see the Docker logs having an"it works" message being output to FluentD.

License

Apache License, Version 2.0.

About NodeJS versions

This package is compatible with NodeJS versions >= 6.

About

A structured logger for Fluentd (Node.js)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors33


[8]ページ先頭

©2009-2025 Movatter.jp