Download the PHP package nekonomokochan/php-json-logger without Composer
On this page you can find all versions of the php package nekonomokochan/php-json-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nekonomokochan/php-json-logger
More information about nekonomokochan/php-json-logger
Files in nekonomokochan/php-json-logger
Package php-json-logger
Short Description LoggingLibrary for PHP. Output by JSON Format
License MIT
Informations about the package php-json-logger
php-json-logger
LoggingLibrary for PHP. Output by JSON Format
This Library is mainly intended for use in web applications.
Getting Started
Install composer package
How To Use
Basic usage
It is output as follows.
The unit of process_time
is ms(millisecond).
How to change output filepath
Default output filepath is /tmp/php-json-logger-yyyy-mm-dd.log
.
If you want to change the output filepath, please set the output filepath to the builder class.
The output filepath is /tmp/test-php-json-logger-yyyy-mm-dd.log
.
It is output as follows.
How to Set trace_id
Any value can be set for trace_id
.
This will help you when looking for logs you want.
It is output as follows.
How to change logLevel
Please use \Nekonomokochan\PhpJsonLogger\LoggerBuilder::setLogLevel()
.
For example, the following code does not output logs.
Because the level is set to CRITICAL
.
You can set the following values for logLevel
.
These are the same as logLevel
defined in Monolog.
- DEBUG = 100
- INFO = 200
- NOTICE = 250
- WARNING = 300
- ERROR = 400
- CRITICAL = 500
- ALERT = 550
- EMERGENCY = 600
How to change channel
Default channel is PhpJsonLogger
.
If you want to change the channel, you can change it with the following code.
For example, the output is as follows.
How to change Log Rotation Date
This is the default setting to save logs for 7 days.
If you want to change the log Rotation date, you can change it with the following code.
The following code sets the log retention period to 2 days.
Extend and use \Nekonomokochan\PhpJsonLogger\JsonFormatter
You can make your own \Monolog\Logger
using only \Nekonomokochan\PhpJsonLogger\JsonFormatter
.
This method is useful when you need \Monolog\Logger
in web application framework(e.g. Laravel).
The following is sample code.
It is output to extended-monolog-test-yyyy-mm-dd.log
as follows
The following code is necessary to output trace_id
and process_time
.
The code below is the code necessary to normally display the stack trace with JSON.
To output the stack trace to the log, execute the following code.
Please pay attention to the part $this->formatPhpJsonLoggerErrorsContext($exception, $context)
.
This is necessary processing to format the error log into JSON and output it.
This is the method implemented in \Nekonomokochan\PhpJsonLogger\ErrorsContextFormat
.
It is output to extended-monolog-test-yyyy-mm-dd.log
as follows.
If you want to know more detailed usage, please look at php-json-logger/tests/ExtendedMonologTest.php
.
Notification To Slack
To send the log to Slack please execute the following code.
This code will be sent to slack if the log level is CRITICAL
or higher.
Use in Docker
Please use LoggerBuilder.setUseInDocker
in order to use it on Docker.
When setUseInDocker is set to true, no file output is done and the log is output as stdout
.
Caution
\Nekonomokochan\PhpJsonLogger\Logger
is a subclass that extends \Monolog\Logger
You can use it like \Monolog\Logger
.
However, for the following methods, you can pass only classes that extend \Exception
or \Error
as arguments.
\Nekonomokochan\PhpJsonLogger\Logger::error()
\Nekonomokochan\PhpJsonLogger\Logger::critical()
\Nekonomokochan\PhpJsonLogger\Logger::alert()
\Nekonomokochan\PhpJsonLogger\Logger::emergency()
In case of violation, \Nekonomokochan\PhpJsonLogger\Logger
will Throw \Nekonomokochan\PhpJsonLogger\InvalidArgumentException
License
MIT