phputil/logger

一个非常简单的日志记录器

1.3 2017-05-06 03:26 UTC

This package is auto-updated.

Last update: 2024-09-23 05:45:50 UTC


README

一个简单的PHP日志记录器。无需魔法配置。只需您最需要的最基本功能。

提供的接口和类

可用的日志方法

  • bool debug( string $message, Exception $e = null, array $context = array() );
  • bool info( string $message, Exception $e = null, array $context = array() );
  • bool warn( string $message, Exception $e = null, array $context = array() );
  • bool error( string $message, Exception $e = null, array $context = array() );
  • bool log( string $logType, string $message, Exception $e = null, array $context = array() );

安装

composer require phputil/logger

示例 1

<?php
require_once 'vendor/autoload.php'; // composer

use phputil\TextFileLogger;

// It is recommended to set the DateTimeZone when using TextFileLogger.
$logger = new TextFileLogger( 'log.txt', false, new \DateTimeZone( 'America/Sao_Paulo' ) );

$logger->info( 'Something will happen' );
$logger->debug( 'Something happened.' );
$logger->warn( 'Wait!' );
$logger->error( 'Ouch.' );

$logger->log( Logger::DEBUG, "That's awesome!" );
?>

示例 2

<?php
require_once 'vendor/autoload.php'; // composer

use phputil\Logger;
use phputil\TextFileLogger;
use phputil\FakeLogger;

$inDebugMode = true;

$logger = $inDebugMode
	? new TextFileLogger( 'log.txt', false, new \DateTimeZone( 'America/Sao_Paulo' ) )
	: new FakeLogger();

$logger->info( 'Something will happen' );
try {
	throw new \Exception( 'Hummm... something bad happened.' );
} catch ( \Exception $e ) {
	// Logs message and trace
	$logger->error( 'Ouch, I did not expect that!', $e );
}

$logger->log( Logger::DEBUG, "That's awesome!" );
?>

示例 3

<?php
require_once 'vendor/autoload.php'; // composer

use phputil\EchoLogger;

$logger = new EchoLogger();
$logger->info( 'It can log to the console too!' );
?>