当你想着鞋垫自己的东西的时候,或者说是整理下过去用过的工具,代码,这时候你就会想到看看github上比较好的代码,这里主要讲PHP的
估计在win上安装composer比较麻烦,或者不好用,但是你想用本地的wamp,那么你可以用虚拟机安装好composer,然后就可以下载你需要的。这里以我正在使用的monolog日志系统
composer require monolog/monolog
这样就会在当前目录生成和下载
vender就是统一的第三方插件的文件夹,composer.json就是配置文件,composer会根据这个去下载第三方,composer.lock会限制composer不会更新你安装过插件的版本,保持你们开发的稳定性
现在来说说monolog,本来就是安装PHP规范写的,文档也写的比较清楚,不过文档看完,你了解了设计思路,但是具体的应用需要自己在使用中慢慢发掘或者专门去测试一下
上代码:
<?php include "vendor/autoload.php"; use Monolog\logger; use Monolog\Handler\StreamHandler; use Monolog\Formatter\LineFormatter; // include "vendor/monolog/monolog/tests/Monolog/LoggerTest.php"; //设定格式的地方 $dateFormat = "Y-n-j, g:i:a"; $output = "%datetime% > %channel%.%level_name% > %message% %context% %extra%\n"; $formatter = new LineFormatter($output, $dateFormat); $log = new Logger(‘myApp‘); //$log = new Monolog\Logger("name"); $bug_log = new StreamHandler(‘logs/development.log‘,logger::DEBUG); $bug_log->setFormatter($formatter); $log->pushHandler($bug_log); $log->pushHandler(new StreamHandler(‘logs/production.log‘,logger::WARNING)); $error = array(‘12‘=>‘jack‘,‘13‘=>‘marry‘); $error_string = ‘a error‘; //$log->debug($error_string); //$log->warning(‘This is a warning. No caring now‘); //$log->info(‘Where Can I see you?‘); // $logtest = new LoggerTest(); // $logtest->testGetName(); $log->addInfo(‘Adding a new user‘, array(‘username‘ => ‘Seldaek‘)); $log->pushProcessor(function ($record) { $record[‘extra‘][‘dummy‘] = ‘Hello world!now I cannot see the world outside.‘; return $record; }); //withName是单独复制,换个名字记录 $securityLogger = $log->withName(‘security‘); $securityLogger->info(‘我涉及到的都是安全问题‘);
针对数据库的写法
上代码
use Monolog\Logger; use Monolog\Handler\AbstractProcessingHandler; class PDOHandler extends AbstractProcessingHandler { private $initialized = false; private $pdo; private $statement; public function __construct(PDO $pdo, $level = Logger::DEBUG, $bubble = true) { $this->pdo = $pdo; parent::__construct($level, $bubble); } protected function write(array $record) { if (!$this->initialized) { $this->initialize(); } $this->statement->execute(array( ‘channel‘ => $record[‘channel‘], ‘level‘ => $record[‘level‘], ‘message‘ => $record[‘formatted‘], ‘time‘ => $record[‘datetime‘]->format(‘U‘), )); } private function initialize() { $this->pdo->exec( ‘CREATE TABLE IF NOT EXISTS monolog ‘ .‘(channel VARCHAR(255), level INTEGER, message LONGTEXT, time INTEGER UNSIGNED)‘ ); $this->statement = $this->pdo->prepare( ‘INSERT INTO monolog (channel, level, message, time) VALUES (:channel, :level, :message, :time)‘ ); $this->initialized = true; }
愿大家一路顺风,更多的学习更多优秀的代码,在使用完,休闲的时候看看人家的思路
愿法界众生,皆得安乐
时间: 2024-10-14 00:33:31