Monolog - Logging for PHP

Monolog sends your logs to files, sockets, inboxes, databases and various web services. See the complete list of handlers below. Special handlers allow you to build advanced logging strategies.

先看使用

Lumen重写日志

  • 首先在bootstrap\app.php中修改:
1
2
3
$app = new App\Application(
realpath(__DIR__.'/../')
);
  • 然后再修改Application
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

// Custom default or hard-coded behavior of lumen
// @caoxl

namespace App;

use Laravel\Lumen\Application as LumenBase;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class Application extends LumenBase
{
// Rewrite log handler
protected function getMonologHandler()
{
return (
new StreamHandler(storage_path(env(
'APP_LOG_PATH',
'logs/' . date('Y-m-d') . '.log'
)),
Logger::DEBUG
))
->setFormatter(new LineFormatter(null, null, true, true));
}
}

TP重写日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

namespace App\Http\Controllers\Common;

use Monolog\Handler\BrowserConsoleHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\SocketHandler;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;

class BGCLogger
{
/**
* 一个简便的日志
* @author caoxl
* @param $name
* @param int $log_level
* @param string $file_name
* @return Logger
*/
public static function getLogger($name, $log_level = Logger::INFO, $file_name = 'bgc.log')
{
$log_socket = C('LOGGING_SOCKET');
$log_debug = intval(C('LOGGING_DEBUG'));
$log_browser = intval(C('LOGGING_BROWSER'));

if (0 != $log_debug) {
$log_level = Logger::DEBUG;
}

$log_path = LOG_PATH . $file_name;
$logger = new Logger($name);
$handler = new RotatingFileHandler($log_path, 10, $log_level);
$module_name = defined('BIND_MODULE') ? BIND_MODULE : 'UNKNOWN';
$formatter = new LineFormatter("%datetime% " . $module_name . ".%channel%.%level_name%: %message%\n", 'Y-m-d H:i:s');
$handler->setFormatter($formatter);
$logger->pushHandler($handler, $log_level);

if (!empty($log_socket)) {
$sock = new SocketHandler($log_socket, $log_level);
if ($sock) {
$sock->setFormatter($formatter);
$sock->setLevel($log_level);
$logger->pushHandler($sock, $log_level);
}
}

if (0 != $log_browser) {
$browser = new BrowserConsoleHandler($log_level);
$browser->setFormatter($formatter);
$browser->setLevel($log_level);
$logger->pushHandler($browser, $log_level);
}

return $logger;
}
}

安装

1
composer require monolog/monolog

日志等级

  • DEBUG (100)
  • INFO (200)
  • NOTICE (250)
  • WARNING (300)
  • ERROR (400)
  • CRITICAL (500)
  • ALERT (550)
  • EMERGENCY (600)

基础使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Mongolog\Handler\FirePHPHandler;

// Create some handlers
$stream = new StreamHandler(__DIR__ . '/my_app.log', Logger::DEBUG);
$firephp = new FirePHPHandler();

// Create the main logger of the app
$logger = new Logger('my_logger');
$logger->pushHandler($stream);
$logger->pushHandler($firephp);

// Create a logger for the security-related stuff with a different channel
$securityLogger = new Logger('security');
$securityLogger->pushHandler($stream);
$securityLogger->pushHandler($firephp);

// Or clone the first one to only change the channel
$securityLogger = $logger->withName('security');

// you can use your logger
$logger->info('My logger is now ready');

自定义日志格式

1
2
3
4
5
6
7
8
9
10
11
12
13
// the default date format is "Y-m-d H:i:s"
$dateFormate = "Y n j, g:i a";
// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
$output = "%datetime% > $level_name% > %message% %content% %extra%\n";
// finally, create a formatter
$formatter = new LineFormatter($output, $dateFormat);

// Create a handler
$stream = new StreamHandler(__DIR__ . '/my_app.log', Logger::DEBUG);
$stream->setFormatter($formatter);
// bind it to a logger object
$securityLogger = new Logger('sucurity');
$securityLogger->pushHandler($stream);

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2023 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :