概览 简单地说,Logback 是一个 Java 领域的日志框架。它被认为是 Log4J 的继承人。Logback 主要由三个模块组成:…
July 20, 2018
php:最好的日志管理 monolog, php写日志monolog, php日志类monolog
主页:https://seldaek.github.io/monolog/
项目:https://github.com/Seldaek/monolog
安装:(什么是composer?)
$ composer require monolog/monolog
简单使用:
<?php use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // add records to the log $log->warning('Foo'); $log->error('Bar');
实例:外套类:
<?php /** * @filesource : logging.php * @Author : GLS * @copyright : Copyright (C) 2010-2012 GLS IT Studio NY * @version : Created on Dec 6, 2010 10:12:19 AM * @Github : https://github.com/Seldaek/monolog * */ /** Log Levels Monolog supports the logging levels described by RFC 5424. DEBUG (100): Detailed debug information. INFO (200): Interesting events. Examples: User logs in, SQL logs. NOTICE (250): Normal but significant events. WARNING (300): Exceptional occurrences that are not errors. Examples: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. ERROR (400): Runtime errors that do not require immediate action but should typically be logged and monitored. CRITICAL (500): Critical conditions. Example: Application component unavailable, unexpected exception. ALERT (550): Action must be taken immediately. Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. EMERGENCY (600): Emergency: system is unusable. use Monolog\Logger; use Monolog\Handler\StreamHandler; <?php // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // add records to the log $log->warning('Foo'); $log->error('Bar'); <?php $logger->info('Adding a new user', array('username' => 'Seldaek')); <?php use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\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'); */ include_once rtrim(dirname(__FILE__), '/').'/vendor/autoload.php'; class Logging { private $levels = array( 100 => 'debug', 200 => 'info', 250 => 'notice', 300 => 'warning', 400 => 'error', 500 => 'critical', 550 => 'alert', 600 => 'emergency', ); private $logger; private $current_level; function __construct($params = array()) { $today_log = '_'.date('Y-m-d').'.log'; $logger_name = $params['name'] ?? 'logger'; $logger_file = $params['file'] ?? str_replace(' ','-',$logger_name).$today_log; if(basename($logger_file) == $logger_file) $logger_file = LOGPATH.basename($logger_file); $logger_file = strtolower($logger_file); if(pathinfo($logger_file, PATHINFO_EXTENSION) != 'log') $logger_file .= $today_log; $this->current_level = $params['level'] ?? 100; $this->logger = new Monolog\Logger($logger_name); switch ($this->current_level) { case 100: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::DEBUG)); break; case 200: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::INFO)); break; case 250: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::NOTICE)); break; case 300: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::WARNING)); break; case 400: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::ERROR)); break; case 500: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::CRITICAL)); break; case 550: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::ALERT)); break; case 600: $this->logger->pushHandler(new Monolog\Handler\StreamHandler($logger_file, Monolog\Logger::EMERGENCY)); break; } return $this; } function log($msg = '', $data = '', $level_code = '') { $level_func = $this->levels[$level_code] ?? false; if($data AND !is_array($data)) $data = json_decode(json_encode($data), true); if($msg AND $level_func AND $level_func) { $this->current_level = $level_code; $data ? $this->logger->{$level_func}($msg, $data) : $this->logger->{$level_func}($msg); } return $this; } function __call($func, $args) { $func = strtolower($func); $level_code = array_search($func, $this->levels); if($level_code) $this->log(($args[0]??''), ((array)$args[1]??array()), ($args[2]??$level_code)); return $this; } } // 用法 $logging = new Logging(); $logging->debug('i am justcode! ', array('subdomain'=>'justcode', 'domain'=>'ikeepstudying.com')); $logging->error('i am justcode! ');
本文:php:最好的日志管理 monolog, php写日志monolog, php日志类monolog