snapsuzun/yii2-logger

Logger for Yii2 that can write to Logstash, file and etc. Also it extend base yii\log\Target to send logs from default Yii2 logger to Logstash.

Installs: 252

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

1.0.0 2019-03-22 05:50 UTC

This package is auto-updated.

Last update: 2020-12-28 15:54:03 UTC


README

An extension for log messages to logstash, file and etc.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist snapsuzun/yii2-logger

or add

"snapsuzun/yii2-logger": "dev-master"

to the require section of your composer.json file.

Configuration

To use this extension for sending logs to logstash, simply add the following code in your application configuration:

return [
    //....
    'components' => [
        'logger' => [
            'class' => \snapsuzun\yii2logger\logstash\Logger::class,
            'host' => 'localhost',
            'port' => 12201,
            'logIndex' => 'test', // Index of created logs in ElasticSearch
            'user' => 'username', // Username and password for authenticate on logstash server if it need authentication for create log
            'password' => 'password',
            'transportType' => \snapsuzun\yii2logger\logstash\LogstashInterface::TRANSPORT_HTTP // Maybe TRANSPORT_HTTP or TRANSPORT_SOCKET
        ],
    ],
];

If you want to send logs to logstash asynchronously you can add component of class snapsuzun\yii2logger\logstash\LoggerAsync:

return [
    //....
    'components' => [
        'logger' => [
            'class' => \snapsuzun\yii2logger\logstash\async\LoggerAsync::class,
            'host' => 'localhost',
            'port' => 12201,
            'logIndex' => 'test', // Index of created logs in ElasticSearch
            'user' => 'username', // Username and password for authenticate on logstash server if it need authentication for create log
            'password' => 'password',
            'transportType' => \snapsuzun\yii2logger\logstash\LogstashInterface::TRANSPORT_HTTP, // Maybe TRANSPORT_HTTP or TRANSPORT_SOCKET
            'queue' => 'queue',
        ],
    ],
];

Asynchronous logger use yii2-queue for sending logs to logstash. You should set queue in configuration that may be link to a component of class \yii\queue\Queue or array with configuration to create object with some class. When asynchronous logger create log it create a job instance og \snapsuzun\yii2logger\logstash\LoggerAsyncSendJob and put its to queue.

Also, you can add target to default Yii2 log:

return [
    //....
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => \snapsuzun\yii2logger\logstash\LogstashTarget::class,
                    'levels' => ['error', 'warning'],
                    'host' => 'localhost',
                    'port' => 12201,
                    'logIndex' => 'test', // Index of created logs in ElasticSearch
                    'user' => 'username', // Username and password for authenticate on logstash server if it need authentication for create log
                    'password' => 'password',
                    'transportType' => \snapsuzun\yii2logger\logstash\LogstashInterface::TRANSPORT_HTTP // Maybe TRANSPORT_HTTP or TRANSPORT_SOCKET
                ]
            ]
        ]
    ]
]

or async target:

return [
    //....
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => \snapsuzun\yii2logger\logstash\async\LogstashAsyncTarget::class,
                    'levels' => ['error', 'warning'],
                    'host' => 'localhost',
                    'port' => 12201,
                    'logIndex' => 'test', // Index of created logs in ElasticSearch
                    'user' => 'username', // Username and password for authenticate on logstash server if it need authentication for create log
                    'password' => 'password',
                    'transportType' => \snapsuzun\yii2logger\logstash\LogstashInterface::TRANSPORT_HTTP, // Maybe TRANSPORT_HTTP or TRANSPORT_SOCKET
                    'queue' => 'queue'
                ]
            ]
        ]
    ]
]

For pushing logs to file add the following code in your application configuration:

return [
    //....
    'components' => [
        'logger' => [
            'class' => \snapsuzun\yii2logger\file\Logger::class
        ],
    ],
];