Fluentd

How to send log messages to Fluentd.

Key takeaways

FluentdLogger Component used to send log messages to Fluentd.
configure() Method used to set the values of several configuration parameters.
open() Method used to connect a logger to Fluentd.
close() Method used to close a logger and free used resources.
setLevel() Method used to set the required log level.
fatal() Method used to log a fatal level or level 1 message.
error() Method used to log an error level or level 2 message.
warn() Method used to log a warning level or level 3 message.
info() Method used to log an info level or level 4 message.
debug() Method used to log a debug level or level 5 message.
trace() Method used to log a trace level or level 6 message.

Introduction

In this tutorial, you will learn how to use the FluentdLogger component to send log messages to Fluentd. First, we will describe this component and understand its pre-requisites. Then, we will create a custom component with methods that generate log messages every time they are called, run it and see the messages received by Fluentd. Finally, we will combine the different code sections into one program and summarize what we have learned.

FluentdLogger

This component provides a tool to send log messages to Fluentd. It extends the CachedLogger class and, at present, doesn’t support authorization. The next sections explain its main methods and how to verify that log messages have been received by Fluentd.

Pre-requisites

In order to use this component, we need to import it first. The following code shows how to do this:

import { FluentdLogger } from 'pip-services4-fluentd-node';
Not available
Not available
Not available
Not available
Not available

Creating a component with a logger

Now that we have imported our Fluentd logger, we can create a custom component that uses it. In our example, we define a custom component that is configurable, openable, and closeable. Thus, we add the IConfigurable and IOpenable interfaces.

Then, we define a logger as an instance of the FluentdLogger class and configure it. The main configuration parameters available from this class are:

figure 1

After this, we set the logging level we want (in our example level 5 or DEBUG) and connect our component to Fluentd via the open() method.

Finally, we add a custom method with an artificial problem and logging messages for the different methods. The resulting code is:

import { ConfigParams, IConfigurable, IOpenable, Context } from "pip-services4-components-node";
import { FluentdLogger } from 'pip-services4-fluentd-node';

class MyComponent implements IConfigurable, IOpenable {
    private _logger = new FluentdLogger();

    public constructor(){
        this._logger.configure(ConfigParams.fromTuples(
            "connection.protocol", "http",
            "connection.host", "localhost",
            "connection.port", 24224
        ));
        this._logger.setLevel(5);
        this._logger.open(null);
        this._logger.info(null, "MyComponent has been created.666666");
        this._logger.debug(null, "Everything is OK.");

    }
    
    public configure(config: ConfigParams): void {

        this._logger.info(null, "MyComponent is configured.")
    }

    public open(ctx: Context): Promise<void> {
        this._logger.info(ctx, "MyComponent is open.");
        return null;
    }

    public close(ctx: Context): Promise<void> {
        this._logger.info(ctx, "MyComponent is closed.");
        // this._logger.close(null);
        return null;
    }


    public isOpen(): boolean {
        
        return true;
    }
    public myMethod(ctx: Context): void {
        // create an artificial problem
        try {
            throw Error('Logging demo. Error created');
        }
        catch (ex) {
            this._logger.error(ctx, ex, "Error created.")
        }

    }
}
Not available
Not available
Not available
Not available
Not available

Generating logs

Now that we have a component with logging capabilities, we create an instance of it and call the different methods. In this manner, we generate log messages that are sent to Fluentd. The following code is an example of how to do this:

    let component = new MyComponent();
   
    component.configure(ConfigParams.fromTuples(
        "parameter", "123"
    ));
    component.open(null);
    component.myMethod(null);
    component.close(null);
Not available
Not available
Not available
Not available
Not available

Our logs in Fluentd

We can see the results on the Fluentd log files, usually located in the fluentd/log directory. The following picture shows the results obtained after running the code shown in the previous sections.

figure 2

Final code

Our last step is to combine all the code from the previous sections into one program. The code below is the result of this operation:

import { ConfigParams, IConfigurable, IOpenable, Context } from "pip-services4-components-node";
import { FluentdLogger } from 'pip-services4-fluentd-node';

class MyComponent implements IConfigurable, IOpenable {
    private _logger = new FluentdLogger();

    public constructor(){
        this._logger.configure(ConfigParams.fromTuples(
            "connection.protocol", "http",
            "connection.host", "localhost",
            "connection.port", 24224
        ));
        this._logger.setLevel(5);
        this._logger.open(null);
        this._logger.info(null, "MyComponent has been created.666666");
        this._logger.debug(null, "Everything is OK.");

    }
    
    public configure(config: ConfigParams): void {

        this._logger.info(null, "MyComponent is configured.")
    }

    public open(ctx: Context): Promise<void> {
        this._logger.info(ctx, "MyComponent is open.");
        return null;
    }

    public close(ctx: Context): Promise<void> {
        this._logger.info(ctx, "MyComponent is closed.");
        // this._logger.close(null);
        return null;
    }


    public isOpen(): boolean {
        
        return true;
    }
    public myMethod(ctx: Context): void {
        // create an artificial problem
        try {
            throw Error('Logging demo. Error created');
        }
        catch (ex) {
            this._logger.error(ctx, ex, "Error created.")
        }

    }


}


export async function main() {

    let component = new MyComponent();
   
    component.configure(ConfigParams.fromTuples(
        "parameter", "123"
    ));
    component.open(null);
    component.myMethod(null);
    component.close(null);
    console.log('Fluentd terminated');

    
}
Not available
Not available
Not available
Not available
Not available

Wrapping up

In this tutorial, we have seen how to use the FluentdLogger class to send log messages to Fluentd. We started by learning the pre-requisites. Then, we created a custom component with methods that send different log messages via the FluentdLogger class, and run them. Finally, we saw the messages received by Fluentd and compiled the code into one program.