Skip to content

Commit

Permalink
新增monolog
Browse files Browse the repository at this point in the history
  • Loading branch information
lhs168 committed Jan 20, 2018
1 parent 9221035 commit 1eb8594
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 212 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"minimum-stability": "stable",
"require": {
"twig/twig": "^1.33.2"
"twig/twig": "^1.33.2",
"monolog/monolog": "^1.23.0"
},
"autoload": {
"psr-4": {
Expand All @@ -23,7 +24,8 @@
"Fasim\\Library\\": "src/Fasim/Library",
"Fasim\\Log\\": "src/Fasim/Log",
"Fasim\\Session\\": "src/Fasim/Session",
"Fasim\\Support\\": "src/Fasim/Cache"
"Fasim\\Support\\": "src/Fasim/Support",
"Fasim\\View\\": "src/Fasim/View"
},
"files": [
"src/Fasim/Support/helpers.php"
Expand Down
66 changes: 63 additions & 3 deletions src/Fasim/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
}

use Fasim\Cache\Cache;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\SyslogHandler;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\AbstractSyslogHandler;

/**
* SLApplication 创建应用的基本类
Expand Down Expand Up @@ -66,6 +72,8 @@ class Application {
*/
private $plugins;

private $configLoggerFunc;

private static $instance;
public static function getInstance() {
if (self::$instance == null) {
Expand Down Expand Up @@ -172,6 +180,57 @@ protected function registerFacdes() {
$this->singleton('cache', function($app) {
return new Cache();
});
$this->singleton('log', function($app) {

$cfg = $this->make('config')->get('logs', ['type' => 'daily']);
$channel = isset($cfg['channel']) ? $cfg['channel'] : 'fasim';
$logger = new Logger('fasim');
if ($this->configLoggerFunc != null) {
call_user_func($this->configLoggerFunc, $logger);
return $logger;
}
$handler = null;
$level = strtoupper(isset($cfg['level']) ? $cfg['level'] : 'warning');
$levels = Logger::getLevels();
$intLevel = isset($levels[$level]) ? $levels[$level] : Logger::WARNING;
$path = '';
if ($type != 'syslog' && $type != 'errorlog') {
$path = isset($cfg['path']) ? $cfg['path'] : 'logs/log.log';
$external = isset($cfg['external']) ? boolval($cfg['external']) : false;
if (!$external) {
$path = APP_DATA_PATH . $path;
}
$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
switch ($cfg['type']) {
case 'single':
$handler = new StreamHandler($path, $intLevel);
break;
case 'syslog':
$ident = isset($cfg['ident']) ? $cfg['ident'] : 'fasim';
$facility = isset($cfg['facility']) ? $cfg['facility'] : LOG_USER;
//AbstractSyslogHandler
$handler = new SyslogHandler($ident, $facility, $intLevel);
break;
case 'errorlog':
$messageType = isset($cfg['message_type']) ? $cfg['message_type'] : 'system';
if ($messageType != 'api') {
$messageType = 'system';
}
$expandNewlines = isset($cfg['expand_newlines']) ? $cfg['expand_newlines'] : false;
$handler = new ErrorLogHandler($messageType, $intLevel, $expandNewlines);
break;
default:
$maxFiles = intval(isset($cfg['max_files']) ? $cfg['max_files'] : '0');
$handler = new RotatingFileHandler($path, $maxFiles, $intLevel);
break;
}
$logger->pushHandler($handler);
return $logger;
});
$this->singleton('security', function($app) {
return new Security();
});
Expand Down Expand Up @@ -212,6 +271,9 @@ public function make($abstract, array $parameters = []) {
}
}

public function configLogger($func) {
$this->configLoggerFunc = $func;
}

/**
* 设置调试模式
Expand Down Expand Up @@ -287,15 +349,13 @@ public function run() {


$debug = $this->make('config')->get('debug') === true;
$traceString = $exception->getTraceAsString();
$traceString = str_replace("\n", "<br />\n", $traceString);
$error = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace(),
'traceString' => $traceString,
'trace' => $exception->getBackTrace()
];
$data = [
'error' => $error,
Expand Down
3 changes: 3 additions & 0 deletions src/Fasim/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Fasim\Core;

use Fasim\View\TwigClassExtension;

/**
* SLController 控制器基类
*/
Expand Down Expand Up @@ -86,6 +88,7 @@ public function __construct($app, $controllerName) {
'charset' => $this->config->get('charset', 'utf-8'),
'debug' => $this->config->get('debug', false)
));
$this->view->addExtension(new TwigClassExtension());



Expand Down
7 changes: 5 additions & 2 deletions src/Fasim/Core/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ public function __construct($message = null, $code = 500, $errFile = null, $errL
$this->line = $errLine;
}

$this->backtrace = $this->getTrace();
if ($exclude) {
$trace = $this->getTrace();
$trace = $this->backtrace;
unset($trace[0]);
ksort($trace);
$this->backtrace = $trace;
}
}

//$this->logError();
public function getBacktrace() {
return $this->backtrace;
}

public static function errorHandler($errno, $errstr, $errfile, $errline) {
Expand Down
20 changes: 20 additions & 0 deletions src/Fasim/Facades/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @copyright Copyright(c) 2012 Fasim
* @author Kevin Lai<[email protected]>
*/

namespace Fasim\Facades;


class Log extends Facade {

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() {
return 'log';
}
}
62 changes: 0 additions & 62 deletions src/Fasim/Log/DBLog.php

This file was deleted.

66 changes: 0 additions & 66 deletions src/Fasim/Log/FileLog.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Fasim/Log/ILog.php

This file was deleted.

44 changes: 0 additions & 44 deletions src/Fasim/Log/LogFactory.php

This file was deleted.

Loading

0 comments on commit 1eb8594

Please sign in to comment.