All Projects → samdark → Yii2 Psr Log Target

samdark / Yii2 Psr Log Target

Licence: bsd-3-clause
Yii 2.0 log target that is able to write messages to PSR-3 compatible logger

Projects that are alternatives of or similar to Yii2 Psr Log Target

Yii2 Telegram Log
Telegram log target for Yii 2
Stars: ✭ 24 (-58.62%)
Mutual labels:  yii2, yii2-extension, logging, log
Yii2 Slack Log
Pretty Slack log target for Yii 2
Stars: ✭ 24 (-58.62%)
Mutual labels:  yii2, yii2-extension, logging, log
Acho
The Hackable Log
Stars: ✭ 189 (+225.86%)
Mutual labels:  logging, log, logger
Logcat
Android 日志打印框架,在手机上可以直接看到 Logcat 日志啦
Stars: ✭ 189 (+225.86%)
Mutual labels:  logging, log, logger
gxlog
A concise, functional, flexible and extensible logger for go.
Stars: ✭ 65 (+12.07%)
Mutual labels:  log, logger, logging
Easylogger
An ultra-lightweight(ROM<1.6K, RAM<0.3k), high-performance C/C++ log library. | 一款超轻量级(ROM<1.6K, RAM<0.3k)、高性能的 C/C++ 日志库
Stars: ✭ 1,968 (+3293.1%)
Mutual labels:  logging, log, logger
Quicklogger
Library for logging on files, console, memory, email, rest, eventlog, syslog, slack, telegram, redis, logstash, elasticsearch, influxdb, graylog, Sentry, Twilio, ide debug messages and throw events for Delphi/Firemonkey/freepascal/.NET (Windows/Linux/OSX/IOS/Android).
Stars: ✭ 137 (+136.21%)
Mutual labels:  logging, log, logger
Golog
A high-performant Logging Foundation for Go Applications. X3 faster than the rest leveled loggers.
Stars: ✭ 208 (+258.62%)
Mutual labels:  logging, log, logger
Android Filelogger
A general-purpose logging library with built-in support to save logs to file efficiently.
Stars: ✭ 70 (+20.69%)
Mutual labels:  logging, log, logger
Plog
Portable, simple and extensible C++ logging library
Stars: ✭ 1,061 (+1729.31%)
Mutual labels:  logging, log, logger
Serverless Es Logs
A Serverless plugin to transport logs to ElasticSearch
Stars: ✭ 51 (-12.07%)
Mutual labels:  logging, log, logger
Serverlog
A simple, practical and innovative Node.js log library that enables you to view logs in Chrome dev tools and browser Console.
Stars: ✭ 117 (+101.72%)
Mutual labels:  logging, log, logger
Node Lambda Log
Basic logging mechanism for Node 6.10+ Lambda Functions
Stars: ✭ 115 (+98.28%)
Mutual labels:  logging, log, logger
Heliumlogger
A lightweight logging framework for Swift
Stars: ✭ 169 (+191.38%)
Mutual labels:  logging, log, logger
Loguru
Python logging made (stupidly) simple
Stars: ✭ 10,510 (+18020.69%)
Mutual labels:  logging, log, logger
Monolog Bundle
Symfony Monolog Bundle
Stars: ✭ 2,532 (+4265.52%)
Mutual labels:  logging, log, logger
Cocoadebug
iOS Debugging Tool 🚀
Stars: ✭ 3,769 (+6398.28%)
Mutual labels:  logging, log, logger
Gollum
An n:m message multiplexer written in Go
Stars: ✭ 883 (+1422.41%)
Mutual labels:  logging, log, logger
Loglevelnext
A modern logging library for Node.js that provides log level mapping to the console
Stars: ✭ 33 (-43.1%)
Mutual labels:  logging, log, logger
Logcustom
A simple log customization tool based on golang 一个基于golang简单的日志定制化工具
Stars: ✭ 46 (-20.69%)
Mutual labels:  log, logger

Yii 2 PSR Log Target

Allows you to process logs using any PSR-3 compatible logger such as Monolog.

Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Quality Score

Installation

composer require "samdark/yii2-psr-log-target"

Usage

In order to use PsrTarget you should configure your log application component like the following:

// $psrLogger should be an instance of PSR-3 compatible logger.
// As an example, we'll use Monolog to send log to Slack.
$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushHandler(new \Monolog\Handler\SlackHandler('slack_token', 'logs', null, true, null, \Monolog\Logger::DEBUG));

return [
    // ...
    'bootstrap' => ['log'],    
    // ...    
    'components' => [
        // ...        
        'log' => [
            'targets' => [
                [
                    'class' => 'samdark\log\PsrTarget',
                    'logger' => $psrLogger,
                    
                    // It is optional parameter. The message levels that this target is interested in.
                    // The parameter can be an array.
                    'levels' => ['info', yii\log\Logger::LEVEL_WARNING, Psr\Log\LogLevel::CRITICAL],
                    // It is optional parameter. Default value is false. If you use Yii log buffering, you see buffer write time, and not real timestamp.
                    // If you want write real time to logs, you can set addTimestampToContext as true and use timestamp from log event context.
                    'addTimestampToContext' => true,
                ],
                // ...
            ],
        ],
    ],
];

Standard usage:

Yii::info('Info message');
Yii::error('Error message');

Usage with PSR logger levels:

Yii::getLogger()->log('Critical message', Psr\Log\LogLevel::CRITICAL);
Yii::getLogger()->log('Alert message', Psr\Log\LogLevel::ALERT);

Usage with original timestamp from context in the log:

// $psrLogger should be an instance of PSR-3 compatible logger.
// As an example, we'll use Monolog to send log to Slack.
$psrLogger = new \Monolog\Logger('my_logger');

$psrLogger->pushProcessor(function($record) {
    if (isset($record['context']['timestamp'])) {
        $dateTime = DateTime::createFromFormat('U.u', $record['context']['timestamp']);
        $timeZone = $record['datetime']->getTimezone();
        $dateTime->setTimezone($timeZone);
        $record['datetime'] = $dateTime;

        unset($record['context']['timestamp']);
    }

    return $record;
});

You can use PsrMessage instead of regular string messages to add custom context.

Standard usage:

Yii::error(new \samdark\log\PsrMessage("Critical message", [
    'custom' => 'context',
    'key' => 'value',
]));

Usage with PSR logger Levels:

Yii::getLogger()->log(new \samdark\log\PsrMessage("Critical message", [
    'important' => 'context'
]), Psr\Log\LogLevel::CRITICAL);

Usage with PSR-3 log message processing:

$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushProcessor(new \Monolog\Processor\PsrLogMessageProcessor());
Yii::debug(new \samdark\log\PsrMessage("Greetings from {fruit}", [
    'fruit' => 'banana'
]));

Running tests

In order to run tests perform the following commands:

composer install
./vendor/bin/phpunit
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].