PHP code example of nekonomokochan / php-json-logger

1. Go to this page and download the library: Download nekonomokochan/php-json-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nekonomokochan / php-json-logger example snippets



use Nekonomokochan\PhpJsonLogger\LoggerBuilder;

$context = [
    'title' => 'Test',
    'price' => 4000,
    'list'  => [1, 2, 3],
    'user'  => [
        'id'   => 100,
        'name' => 'keitakn',
    ],
];

$loggerBuilder = new LoggerBuilder();
$logger = $loggerBuilder->build();
$logger->info('🐱', $context);


use Nekonomokochan\PhpJsonLogger\LoggerBuilder;

$fileName = '/tmp/test-php-json-logger.log';

$context = [
    'cat'    => '🐱',
    'dog'    => '🐢',
    'rabbit' => '🐰',
];

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setFileName($fileName);
$logger = $loggerBuilder->build();
$logger->info('testSetLogFileName', $context);


use Nekonomokochan\PhpJsonLogger\LoggerBuilder;

$context = [
    'name' => 'keitakn',
];

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setTraceId('MyTraceID');
$logger = $loggerBuilder->build();
$logger->info('testSetTraceIdIsOutput', $context);


use Nekonomokochan\PhpJsonLogger\LoggerBuilder;

$context = [
    'cat'    => '🐱',
    'dog'    => '🐢',
    'rabbit' => '🐰',
];

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setLogLevel(LoggerBuilder::CRITICAL);
$logger = $loggerBuilder->build();
$logger->info('testSetLogLevel', $context);

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setChannel('My Favorite Animals');

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setMaxFiles(2);


use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Processor\IntrospectionProcessor;
use Nekonomokochan\PhpJsonLogger\JsonFormatter;

$logFileName = '/tmp/extended-monolog-test.log';

// create extendedMonolog Instance
$formatter = new JsonFormatter();

$rotating = new RotatingFileHandler(
    $logFileName,
    7,
    Logger::INFO
);
$rotating->setFormatter($formatter);

$introspection = new IntrospectionProcessor(
    Logger::INFO,
    ['Nekonomokochan\\PhpJsonLogger\\'],
    0
);

$extraRecords = function ($record) {
    $record['extra']['trace_id'] = 'ExtendedMonologTestTraceId';
    $record['extra']['created_time'] = microtime(true);

    return $record;
};

$extendedMonolog = new Logger(
    'ExtendedMonolog',
    [$rotating],
    [$introspection, $extraRecords]
);

// output info log
$context = [
    'cat'    => '🐱',
    'dog'    => '🐢',
    'rabbit' => '🐰',
];

$extendedMonolog->info('outputInfoLogTest', $context);


$extraRecords = function ($record) {
    $record['extra']['trace_id'] = 'ExtendedMonologTestTraceId';
    $record['extra']['created_time'] = microtime(true);

    return $record;
};


$introspection = new IntrospectionProcessor(
    Logger::INFO,
    ['Nekonomokochan\\PhpJsonLogger\\'],
    0
);


$exception = new \Exception('ExtendedMonologTest.outputErrorLog', 500);
$context = [
    'cat'    => '🐱(=^・^=)🐱',
    'dog'    => '🐢Uoο½₯ο½ͺο½₯oU🐢',
    'rabbit' => '🐰🐰🐰',
];

$extendedMonolog->error(
    get_class($exception),
    $this->formatPhpJsonLoggerErrorsContext($exception, $context)
);


use Nekonomokochan\PhpJsonLogger\LoggerBuilder;
use Nekonomokochan\PhpJsonLogger\SlackHandlerBuilder;

$exception = new \Exception('TestException', 500);
$context = [
    'name'  => 'keitakn',
    'email' => '[email protected]',
];

$slackToken = 'YOUR_SLACK_TOKEN';
$slackChannel = 'YOUR_SLACK_CHANNEL';

$slackHandlerBuilder = new SlackHandlerBuilder($slackToken, $slackChannel);
$slackHandlerBuilder->setLevel(LoggerBuilder::CRITICAL);

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setFileName($this->outputFileBaseName);
$loggerBuilder->setSlackHandler($slackHandlerBuilder->build());
$logger = $loggerBuilder->build();
$logger->critical($exception, $context);


use Nekonomokochan\PhpJsonLogger\LoggerBuilder;

$exception = new \Exception('TestException', 500);
$context = [
    'name'  => 'keitakn',
    'email' => '[email protected]',
];

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setFileName($this->outputFileBaseName);
$loggerBuilder->setUseInDocker(true);
$logger = $loggerBuilder->build();
$logger->critical($exception, $context);

composer 
json
{
    "log_level": "INFO",
    "message": "🐱",
    "channel": "PhpJsonLogger",
    "trace_id": "35b627ce-55e0-4729-9da0-fbda2a7d817d",
    "file": "\/home\/vagrant\/php-json-logger\/tests\/LoggerTest.php",
    "line": 42,
    "context": {
        "title": "Test",
        "price": 4000,
        "list": [
            1,
            2,
            3
        ],
        "user": {
            "id": 100,
            "name": "keitakn"
        }
    },
    "remote_ip_address": "127.0.0.1",
    "server_ip_address": "127.0.0.1",
    "user_agent": "unknown",
    "datetime": "2018-06-04 17:21:03.631409",
    "timezone": "Asia\/Tokyo",
    "process_time": 631.50811195373535
}
json
{
    "log_level": "INFO",
    "message": "testSetLogFileName",
    "channel": "PhpJsonLogger",
    "trace_id": "20f39cdb-dbd8-470c-babd-093a2974d169",
    "file": "\/home\/vagrant\/php-json-logger\/tests\/LoggerTest.php",
    "line": 263,
    "context": {
        "cat": "🐱",
        "dog": "🐢",
        "rabbit": "🐰"
    },
    "remote_ip_address": "127.0.0.1",
    "server_ip_address": "127.0.0.1",
    "user_agent": "unknown",
    "datetime": "2018-06-05 11:28:03.214995",
    "timezone": "Asia\/Tokyo",
    "process_time": 215.09790420532227
}
json
{
    "log_level": "INFO",
    "message": "testSetTraceIdIsOutput",
    "channel": "PhpJsonLogger",
    "trace_id": "MyTraceID",
    "file": "\/home\/vagrant\/php-json-logger\/tests\/LoggerTest.php",
    "line": 214,
    "context": {
        "name": "keitakn"
    },
    "remote_ip_address": "127.0.0.1",
    "server_ip_address": "127.0.0.1",
    "user_agent": "unknown",
    "datetime": "2018-06-05 11:36:02.394269",
    "timezone": "Asia\/Tokyo",
    "process_time": 394.35911178588867
}
json
{
    "log_level": "INFO",
    "message": "testCanSetChannel",
    "channel": "My Favorite Animals",
    "trace_id": "4b8aa070-a533-4376-9bf5-270c8fcc6d87",
    "file": "\/home\/vagrant\/php-json-logger\/tests\/Logger\/LoggerTest.php",
    "line": 347,
    "context": {
        "animals": "🐱🐢🐰🐱🐹"
    },
    "remote_ip_address": "127.0.0.1",
    "server_ip_address": "127.0.0.1",
    "user_agent": "unknown",
    "datetime": "2018-06-07 17:56:48.538117",
    "timezone": "Asia\/Tokyo",
    "process_time": 538.48695755004883
}
json
{
    "log_level": "INFO",
    "message": "outputInfoLogTest",
    "channel": "ExtendedMonolog",
    "trace_id": "ExtendedMonologTestTraceId",
    "file": "\/home\/vagrant\/php-json-logger\/tests\/ExtendedMonologTest.php",
    "line": 85,
    "context": {
        "cat": "🐱",
        "dog": "🐢",
        "rabbit": "🐰"
    },
    "remote_ip_address": "127.0.0.1",
    "server_ip_address": "127.0.0.1",
    "user_agent": "unknown",
    "datetime": "2018-06-06 17:14:26.042013",
    "timezone": "Asia\/Tokyo",
    "process_time": 0.1678466796875
}
json
{
    "log_level": "ERROR",
    "message": "Exception",
    "channel": "PhpJsonLogger",
    "trace_id": "ExtendedMonologTestTraceId",
    "file": "\/home\/vagrant\/php-json-logger\/tests\/ExtendedMonologTest.php",
    "line": 126,
    "context": {
        "cat": "🐱(=^・^=)🐱",
        "dog": "🐢Uoο½₯ο½ͺο½₯oU🐢",
        "rabbit": "🐰🐰🐰"
    },
    "remote_ip_address": "127.0.0.1",
    "server_ip_address": "127.0.0.1",
    "user_agent": "unknown",
    "datetime": "2018-06-06 17:37:57.440757",
    "timezone": "Asia\/Tokyo",
    "process_time": 0.16093254089355469,
    "errors": {
        "message": "ExtendedMonologTest.outputErrorLog",
        "code": 500,
        "file": "\/home\/vagrant\/php-json-logger\/tests\/ExtendedMonologTest.php",
        "line": 117,
        "trace": [
            "#0 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php(1145): Nekonomokochan\\Tests\\ExtendedMonologTest->outputErrorLog()",
            "#1 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php(840): PHPUnit\\Framework\\TestCase->runTest()",
            "#2 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestResult.php(645): PHPUnit\\Framework\\TestCase->runBare()",
            "#3 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php(798): PHPUnit\\Framework\\TestResult->run()",
            "#4 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestSuite.php(776): PHPUnit\\Framework\\TestCase->run()",
            "#5 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/TextUI\/TestRunner.php(529): PHPUnit\\Framework\\TestSuite->run()",
            "#6 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/TextUI\/Command.php(198): PHPUnit\\TextUI\\TestRunner->doRun()",
            "#7 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/TextUI\/Command.php(151): PHPUnit\\TextUI\\Command->run()",
            "#8 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/phpunit(53): PHPUnit\\TextUI\\Command::main()"
        ]
    }
}