samayo/autolog
Composer 安装命令:
composer require samayo/autolog
包简介
A simple PHP logger library
关键字:
README 文档
README
A PHP class to save/log/mail errors/notifications from your app or from /var/log/ as they appear.
Install
Using git
$ git clone https://github.com/samayo/autolog.git
Using composer
$ composer require samayo/autolog
Usage
Short Example.
To quickly email a user activity log
require __DIR__ . "/src/Logger.php"; $log = new Autolog\Logger(["email" => "user@domain.tld"]); if($comment){ $log->log("$user just commented \n $comment", $log::INFO, $log::EMAIL); }
The $log->log() method accepts 4 arguments, but only the first $msg is required.
/** * $msg (required) the actual content to send/log * $type (optional) the message type: error, info, notification.. * $handler (optional) where to send it (db, email, file log) * $verbosity (optional) log as simple or verbose info */ log($msg, $type, $handler, $verbosity);
Available log types, handlers, and verbosity
$type::INFO; // for simple tasks $type::ERROR; // for errors ex: 404 .. $type::ALERT; // for fatal errors or suspicious activity $handler::EMAIL; // send to email $handler::FILE; // write to file $handler::DATABASE; // insert to database $handler::SMS; // send to sms (not yet implemented) $verbosity::SIMPLE; // send simplified log $verbosity::VERBOSE; // send every log information // the below will log an error, in verbose format and mail it $log = new Autolog\Logger(["email" => "user@domain.tld"]); $log->log($msg, $log::ERROR, $log::EMAIL, $log::VERBOSE);
By passing only the first arg: $log->log($msg) the log will tread as ERROR, EMAIL, VERBOSE
Examples
Sending logs to your email
$log = new Autolog\Logger; $log["email"] = "user@domain.tld"; // add email // or add your email list this: $log = new Autolog\Logger(["email" => "user@domain.tld"]); // then log it! if($something){ $log->log("something"); // email 'something' }
Logging to file
To log to a file, you need to pass a writable file to error.log
$log = new Autolog\Logger(["error.log" => __DIR__ . "/mylogs.txt"]); $log->log("ERROR: $error", $log::INFO, $log::FILE); // don't forget $log::FILE
Inserting to database
To store your logs in a database, create a db with these schema
--- database name can be anything, but table and columns should be as seen below CREATE DATABASE IF NOT EXISTS autolog; USE autolog; CREATE TABLE `logs` ( `id` INT NOT NULL AUTO_INCREMENT, `time` DATETIME DEFAULT NULL, `subject` VARCHAR(255) DEFAULT NULL, `level` VARCHAR(50) DEFAULT NULL, `message` TEXT, PRIMARY KEY (id) ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4
Then log your info/error after calling the pdo() PDO object
$log = new Autolog\Logger; $log->pdo(new \PDO( // your pdo host, db, pass here )); $log->log("simple log", $log::ERROR, $log::DATABASE);
Method chaining
You can even quickly chain methods as:
(new \Autolog\Logger) ->pdo(new PDO(/**/))->log("user: $user modified his/her profile", $log::INFO, $log::DATABASE);
Handling Exceptions/Errors
To log all your exceptions/errors use example below:
$logger = Autolog\Logger(["email" => "user@example.com"]); // mail all thrown exceptions set_exception_handler(function($e) use($logger){ $logger->log($e, $log::ERROR, $log::EMAIL); }); // mail all errors set_error_handler(function($no, $str, $file, $line) use ($logger){ $logger->log("Your site has error: $str in file $file at line $line", $log::ERROR, $log::EMAIL); })
Autologs (via cronjob)
To automatically detect log file changes and log messages, use watch() method.
// always watch new errors that appear in (nginx, php) log files $log->watch(true);
To watch new logs and get notified, place the watch() method in it's own file like: log_mailer.php
// log_mailer.php require __DIR__ . "/src/Logger.php"; (new Autolog\Logger([ "nginx.log" => "/var/log/nginx/error.log", "php-fpm.log" => "/var/log/php-fpm/error.log", "mariadb.log" => "/var/log/mariadb/mariadb.log", "access.log" => "access.txt", "email" => "user@example.com" ]))->watch(true);
Now, you can set a cronjob that executes the above script every min/hour then you'll get a new mail everytime a new error is logged in /var/log/...
It is important give the access.log a file where the last time like: nginx.log is accessed.
This is because to detect new error, you must store the timestamp of the last time we check nginx.log
So, if the mtime for nginx file is not the same as we have stored, it means a new log is found ~
License: MIT
samayo/autolog 适用场景与选型建议
samayo/autolog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 152 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「error」 「notification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 samayo/autolog 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samayo/autolog 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 samayo/autolog 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package makes it easy to send notifications via AfricasTalking with Laravel
Apple Push Notification & Feedback Provider
Throttle notifications on a per-channel basis
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
PHP Error Handler module that captures and displays all throwable errors in a given format, making debugging easier and more efficient
统计信息
- 总下载量: 152
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-09