codeincode/simple-logger

该包的最新版本(dev-master)没有可用的许可证信息。

PHP即时日志记录器

dev-master 2017-08-24 15:15 UTC

This package is not auto-updated.

Last update: 2024-09-20 01:46:59 UTC


README

使用此类可以在运行时创建简单的日志文件或日志(信息、警告、错误、异常)。

功能

  • 即时记录日志并打印JSON字符串(启用调试模式)
  • 将日志存储在自定义文件夹和文件中
  • 按日期时间格式划分日志,您可以每天或每秒创建一个日志文件,所有日志都按时间格式组织

安装

要使用SimpleLogger,您只需要下载仓库,将其放入您项目的根目录中,然后

    <?php
     
    require_once "SimpleLogger/SimpleLoader.php";
    
    use SimpleLogger\Simple as Simple;

自定义

SimpleConfigs.php中,您可以编辑以下变量

     <?php

      /**
    	 *  Log folder name
    	 */
    	const LOG_FILE_FOLDER = "simple_log";
    	
    	/**
    	 *  Log file extension
    	 */
    	const LOG_FILE_FORMAT = ".log";
    	
    	/**
    	 *  Date format of the log file name
    	 *  @important this will be part of the file name, take care of the "/" and "."
    	 */
    	const LOG_FILE_DATE_FORMAT = "Y_m_d";
    	
    	/**
    	 *  Date format in the log details
    	 */
    	const LOG_DATE_FORMAT = "Y/m/d H:i:s";
    	
    	/**
    	 *  Timezone for the log
    	 */
    	const LOG_TIMEZONE = "UTC";
    	
    	/**
    	 * Debug mode enabled
    	 *
    	 * @default true
    	 */
    	const LOG_IS_DEBUG = true;
    	
    	/**
    	 *  Custom empty var message
    	 */
    	const LOG_DEFAULT_NO_MESSAGE = "No message";
    	
    	/**
    	 * Custom empty var message
    	 */
    	const LOG_DEFAULT_NO_PARAMETERS = "No parameters";
    	
    	/**
    	 *  String to define log type INFO
    	 */
    	const LOG_TYPE_INFO = "INFO";
    	
    	/**
    	 * String to define log type WARNING
    	 */
    	const LOG_TYPE_WARNING = "WARNING";
    	
    	/**
    	 *  String to define log type ERROR
    	 */
    	const LOG_TYPE_ERROR = "ERROR";
    	
    	/**
    	 *  String to define log type EXCEPTION
    	 */
    	const LOG_TYPE_EXCEPTION = "EXCEPTION";

基本用法

    <?php
    
    $log = Simple::logger();
    
    //Info log with custom message, optional string parameter and the file where the log function is called
    $log->info( "Info Message" , "Optional param" , __FILE__ );
    
    //Error log with custom message and optional array parameter
    $log->error( "Error Message" , array ( "optional_params" => "value" ) );
    
    //Warning log with custom message, optional array parameter and the file where the log function is called and the optional method
    $log->warning( "Warning Message" , array ( "key" => "value" ) , __FILE__ , __METHOD__ );
    
    //Exception log based on standard exception class
    try {
    	throw new Exception( "Dummy Exception" );
    } catch ( Exception $exception ) {
    	$log->exception( $exception , __METHOD__ );
    }
    
    //Returns the instance log array
    $log->getLog();
    

结果

如果您启用调试模式,可以在屏幕上看到即时JSON日志打印,如下所示

                                {
                                    "time": "2017\/02\/23 14:46:43",
                                    "type": "ERROR",
                                    "filename": null,
                                    "method": null,
                                    "message": "Error Message",
                                    "params": {
                                        "optional_params": "value"
                                    }
                                }
                                

您也可以在自动创建的"simple_log"文件夹中看到日志,如下所示

    			======================= START ======================= 
                        
                                    Type: ERROR
                                    Time: 2017/02/23 14:46:43
                                    File: 
                                    Method: 
                                    Message: Error Message
                                    Params: {
                                        "optional_params": "value"
                                    }
    
    			======================= END ==========================

##示例您可以使用此示例来了解SimpleLogger的工作原理