GithubHelp home page GithubHelp logo

monolog's Introduction

pianzhou/monolog

安装

You can add this library as a local, per-project dependency to your project using Composer:

composer require pianzhou/monolog

一、Database Handler介绍

首先需要创建一下数据表,以MySQL为例:

CREATE TABLE IF NOT EXISTS `t_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` text NOT NULL,
  `channel` varchar(20) NOT NULL,
  `level_name` varchar(20) NOT NULL,
  `level` smallint(6) NOT NULL,
  `context` text NOT NULL,
  `extra` text NOT NULL,
  `datetime` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
COMMIT;

目前Database Handler支持PDO实例和laravel ConnectionInterface实例

1、PDO实例使用方法:

$dbHandler     = new \Pianzhou\Monolog\Handler\DatabaseHandler(
    new PDO('mysql:host=localhost;dbname=test', 'root', ''),
    't_logs',
    \Monolog\Logger::DEBUG,
    true
 );

 $dbHandler->setFormatter(new \Pianzhou\Monolog\Formatter\TransformScalarFormatter());

 $logger    = new \Monolog\Logger('测试', [
     $dbHandler,
 ]);

$logger->info('测试一下写入数据库');

2、laravel ConnectionInterface实例使用方法(需要安装依赖:illuminate/database)

$dbHandler     = new \Pianzhou\Monolog\Handler\DatabaseHandler(
    new Illuminate\Database\MySqlConnection(
      new PDO('mysql:host=localhost;dbname=test', 'root', '')
    ),
    't_logs',
    \Monolog\Logger::DEBUG,
    true
 );

 $dbHandler->setFormatter(new \Pianzhou\Monolog\Formatter\TransformScalarFormatter());

 $logger    = new \Monolog\Logger('测试', [
     $dbHandler,
 ]);

$logger->info('测试一下写入数据库');

通过上面的demo,我们就可以很轻松的将日志文件记录到数据库中了。另外,如果使用BufferHandler批量化写入日志的话,DatabaseHandler会一次性批量化的将日志记录写入数据库,而不是一条条记录。

相关链接:

基于PHP Monolog,打造数据库日志记录器(DatabaseHandler)

基于PHP Monolog,打造一个API请求日志记录器

monolog's People

Contributors

pianzhou2021 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.