agarage/ulog

There is no license information available for the latest version (dev-master) of this package.

A PHP Logger

dev-master 2016-05-09 04:52 UTC

README

PHP日志组件,可以使用多种存储方式记录日志,并且可以自由扩展。

使用方法

use AGarage\ULog\ULog as ULog;
use AGarage\ULog\Writer\DoctrineStorage as DoctrineStorage;
use Psr\Log\LogLevel as LogLevel;

$config = [
    'host' => 'localhost',
    'service' => 'ULog test',
    'storages' => [
        [
            'class' => 'AGarage\ULog\Storage\SingleFileStorage',
            'level' => ULog::INFO,
            'path' => '/tmp/ulog.log'
        ]
    ]
];

//初始化单例模式ULog
ULog::initialize($config);
//获取单例模式ULog
$logger = ULog::getLogger();

$anotherLogger = new ULog($config);

//初始化一个数据库连接用于ULog
$conn = \Doctrine\DBAL\DriverManager::getConnection([
    'driver' => 'pdo_mysql',
    'host' => 'localhost',
    'user' => 'root',
    'password' => '123456',
    'dbname' => 'app',
    'charset' => 'utf8'
]);

//创建一个DoctrineStorage
$storage = new DoctrineStorage([
    'level' => LogLevel::INFO
]);
//为DoctrineStorage设置数据库连接
$storage->setConnection($conn);

//向ULog添加Storage
$logger->addStorage($storage);

$logger->info('This log will be written by storages in $logger.');
$anotherLogger->debug('This log will not written by storages in $anotherLogger.');

配置说明

ULog

[
    'host' => 'localhost',  //产生日志的主机名(默认localhost)
    'service' => 'ULog'     //产生日志的服务名(默认ULog)
    'storages' => [          //Writers
        [
            'class' => 'AGarage\ULog\Storage\SingleFileStorage',  //Storage的类名(必须)
            'level' => LogLevel::INFO   //大于该等级的日志将被记录(默认DEBUG)
            'path' => '/tmp/ulog.log'   //日志文件路径(必须)
        ]
    ]
]

Storages

SingleFileStorage

使用单个文本日志文件记录日志

[
    'class' => 'AGarage\ULog\Storage\SingleFileStorage',  //Writer的类名(必须)
    'level' => LogLevel::INFO   //大于该等级的日志将被记录(默认DEBUG)
    'path' => '/tmp/ulog.log'   //日志文件路径(必须)
]

DoctrineStorage

使用Doctrine记录日志

[
    'class' => 'AGarage\ULog\Storage\DoctrineStorage',
    'level' => LogLevel::INFO   //默认DEBUG
]

初始化该类型Storage后需要调用setConnection方法为其设置数据库连接