本文整理汇总了PHP中Phalcon\Logger\Adapter\File::setFormatter方法的典型用法代码示例。如果您正苦于以下问题:PHP File::setFormatter方法的具体用法?PHP File::setFormatter怎么用?PHP File::setFormatter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\Logger\Adapter\File
的用法示例。
在下文中一共展示了File::setFormatter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* インスタンスを生成
* Logger constructor.
* @param null $fileName ファイル名を指定。デフォルトはmain設定ファイルのlog_file_format
*/
public function __construct($fileName = null)
{
if ((bool) $fileName) {
$logFile = LOGS_PATH . $fileName;
} else {
$logFile = LOGS_PATH . APPS_MAIN_CONF['log_file_name_format'];
}
$this->logger = new File($logFile);
$req = new Request();
$uri = $req->getURI();
$this->logger->setFormatter(new LogFormatter("[%date%][{$uri}][%type%] %message%"));
}
示例2: log
/**
* 保存日志
* @param string $logString 日志信息
* @param string $level 日志级别
*/
public function log($logString, $level = 'info')
{
$logger = new FileLogger($this->logDir . $this->logFile);
$lineFormatter = new LineFormatter();
$lineFormatter->setDateFormat('Y-m-d H:i:s');
$logger->setFormatter($lineFormatter);
$logger->log($logString, $this->log_level[$level]);
}
示例3: _initLogger
/**
* Init logger.
*
* @param DI $di Dependency Injection.
* @param Config $config Config object.
*
* @return void
*/
protected function _initLogger($di, $config)
{
if ($config->application->logger->enabled) {
$di->set('logger', function ($file = 'main', $format = null) use($config) {
$logger = new File($config->application->logger->path . APPLICATION_STAGE . '.' . $file . '.log');
$formatter = new FormatterLine($format ? $format : $config->application->logger->format);
$logger->setFormatter($formatter);
return $logger;
}, false);
}
}
示例4: registerServices
/**
* Registers services related to the module
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
/**
* Read configuration
*/
$config = (include APP_PATH . "/apps/backend/config/config.php");
/**
* Setting up the view component
*/
$di['view'] = function () use($config) {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => __DIR__ . '/cache/', 'compiledSeparator' => '_'));
$compiler = $volt->getCompiler();
// format number
$compiler->addFilter('number', function ($resolvedArgs) {
return 'Helpers::number(' . $resolvedArgs . ');';
});
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
};
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
$config = $config->database->toArray();
$dbAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config['adapter'];
unset($config['adapter']);
return new $dbAdapter($config);
};
/**
* Logger service
*/
$di->set('logger', function ($filename = null, $format = null) use($config) {
$format = $format ?: $config->get('logger')->format;
$filename = trim($filename ?: $config->get('logger')->filename, '\\/');
$path = rtrim($config->get('logger')->path, '\\/') . DIRECTORY_SEPARATOR;
$formatter = new FormatterLine($format, $config->get('logger')->date);
$logger = new FileLogger($path . $filename);
$logger->setFormatter($formatter);
$logger->setLogLevel($config->get('logger')->logLevel);
return $logger;
});
$di->set('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri("/backend/");
return $url;
});
}
示例5: testLoggerFormatterNewFormatFormatsDateCorrectly
/**
* Tests new format logs correctly
*
* @author Nikos Dimopoulos <nikos@phalconphp.com>
* @since 2012-09-17
*/
public function testLoggerFormatterNewFormatFormatsDateCorrectly()
{
$fileName = newFileName('log', 'log');
$logger = new PhTLoggerAdapterFile($this->logPath . $fileName);
$formatter = new PhLoggerFormatterLine('%type%|%date%|%message%');
$logger->setFormatter($formatter);
$logger->log('Hello');
$logger->close();
$contents = file($this->logPath . $fileName);
$message = explode('|', $contents[0]);
cleanFile($this->logPath, $fileName);
$date = new \DateTime($message[1]);
$expected = date('Y-m-d H');
$actual = $date->format('Y-m-d H');
$this->assertEquals($expected, $actual, 'Date format not set properly');
}
示例6: testLoggerFormatterLineNewFormatLogsCorrectly
/**
* Tests new format logs correctly
*
* @author Nikos Dimopoulos <nikos@phalconphp.com>
* @since 2012-09-17
*/
public function testLoggerFormatterLineNewFormatLogsCorrectly()
{
$this->specify("Line formatted does not set format correctly", function () {
$I = $this->tester;
$fileName = $I->getNewFileName('log', 'log');
$logger = new File($this->logPath . $fileName);
$formatter = new Line('%type%|%date%|%message%');
$logger->setFormatter($formatter);
$logger->log('Hello');
$logger->close();
$I->amInPath($this->logPath);
$I->openFile($fileName);
$I->seeInThisFile(sprintf('DEBUG|%s|Hello', date('D, d M y H:i:s O')));
$I->deleteFile($fileName);
});
}
示例7: request_start_log
/**
* 记录起始请求日志
* 记录成功返回true,失败或没记录日志返回false
*
* @return bool
*/
public static function request_start_log()
{
if (!BaseLog::isLog('debug')) {
return false;
}
if (!Config::getEnv("app.request_start_log")) {
return false;
}
$data = Request::nonPostParam();
if (Config::getEnv("app.request_log_post")) {
$data = Request::param();
}
$file_path = BaseLog::handle_log_file('framework', 'debug');
$log_time = date("Y-m-d H:i:s", QP_RUN_START);
$ip = Request::getIp();
$router_url = \Qp\Kernel\Http\Router\QpRouter::getRouterStr();
$prefix = "[{$log_time}] [{$ip}] [router : {$router_url}] ";
$msg = "【请求日志】" . json_encode(['data' => $data]);
$logger = new FileAdapter($file_path);
$logger->setFormatter(new LineFormatter("%message%"));
return (bool) $logger->log($prefix . $msg);
}
示例8: initLogger
/**
* Initialize the Logger.
*
* @param DiInterface $di Dependency Injector
* @param Config $config App config
* @param EventsManager $em Events Manager
*
* @return void
*/
protected function initLogger(DiInterface $di, Config $config, EventsManager $em)
{
ErrorHandler::register();
$di->set('logger', function ($filename = null, $format = null) use($config) {
$format = $format ?: $config->get('logger')->format;
$filename = trim($filename ?: $config->get('logger')->filename, '\\/');
$path = rtrim($config->get('logger')->path, '\\/') . DIRECTORY_SEPARATOR;
$formatter = new FormatterLine($format, $config->get('logger')->date);
$logger = new FileLogger($path . $filename);
$logger->setFormatter($formatter);
$logger->setLogLevel($config->get('logger')->logLevel);
return $logger;
});
}
示例9: handle
/**
* Logs the error and dispatches an error controller.
*
* @param \Phalcon\Error\Error $error
* @return mixed
*/
public static function handle(Error $error)
{
$di = Di::getDefault();
$config = $di->getShared('config')->error->toArray();
$logger = $config['logger'];
if (!$logger instanceof AdapterInterface) {
$logger = new FileLogger($logger);
}
$type = static::getErrorType($error->type());
$message = "{$type}: {$error->message()} in {$error->file()} on line {$error->line()}";
if (isset($config['formatter'])) {
$formatter = null;
if ($config['formatter'] instanceof Formatter) {
$formatter = $config['formatter'];
} elseif (is_array($config['formatter'])) {
$format = null;
$dateFormat = null;
if (isset($config['formatter']['format'])) {
$format = $config['formatter']['format'];
}
if (isset($config['formatter']['dateFormat'])) {
$dateFormat = $config['formatter']['dateFormat'];
} elseif (isset($config['formatter']['date_format'])) {
$dateFormat = $config['formatter']['date_format'];
} elseif (isset($config['formatter']['date'])) {
$dateFormat = $config['formatter']['date'];
}
$formatter = new FormatterLine($format, $dateFormat);
}
if ($formatter) {
$logger->setFormatter($formatter);
}
}
$logger->log(static::getLogType($error->type()), $message);
switch ($error->type()) {
case E_WARNING:
case E_NOTICE:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
case E_USER_NOTICE:
case E_STRICT:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_ALL:
break;
case 0:
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
if ($di->has('view')) {
$dispatcher = $di->getShared('dispatcher');
$view = $di->getShared('view');
$response = $di->getShared('response');
$dispatcher->setControllerName($config['controller']);
$dispatcher->setActionName($config['action']);
$dispatcher->setParams(['error' => $error]);
$view->start();
$dispatcher->dispatch();
$view->render($config['controller'], $config['action'], $dispatcher->getParams());
$view->finish();
return $response->setContent($view->getContent())->send();
} else {
echo $message;
}
}
}
示例10: run
public static function run(DiInterface $di, array $options = [])
{
$memoryUsage = memory_get_usage();
$currentTime = microtime(true);
/**
* The app path
*/
if (!defined('K_PATH')) {
define('K_PATH', dirname(dirname(dirname(__FILE__))));
}
/**
* We will need the Utils class
*/
require_once K_PATH . '/library/Kitsune/Utils.php';
/**
* Utils class
*/
$utils = new Utils();
$di->set('utils', $utils, true);
/**
* Check if this is a CLI app or not
*/
$cli = $utils->fetch($options, 'cli', false);
if (!defined('K_CLI')) {
define('K_CLI', $cli);
}
$tests = $utils->fetch($options, 'tests', false);
if (!defined('K_TESTS')) {
define('K_TESTS', $tests);
}
/**
* The configuration is split into two different files. The first one
* is the base configuration. The second one is machine/installation
* specific.
*/
if (!file_exists(K_PATH . '/var/config/base.php')) {
throw new \Exception('Base configuration files are missing');
}
if (!file_exists(K_PATH . '/var/config/config.php')) {
throw new \Exception('Configuration files are missing');
}
/**
* Get the config files and merge them
*/
$base = (require K_PATH . '/var/config/base.php');
$specific = (require K_PATH . '/var/config/config.php');
$combined = array_replace_recursive($base, $specific);
$config = new Config($combined);
$di->set('config', $config, true);
$config = $di->get('config');
/**
* Check if we are in debug/dev mode
*/
if (!defined('K_DEBUG')) {
$debugMode = boolval($utils->fetch($config, 'debugMode', false));
define('K_DEBUG', $debugMode);
}
/**
* Access to the debug/dev helper functions
*/
if (K_DEBUG) {
require_once K_PATH . '/library/Kitsune/Debug.php';
}
/**
* We're a registering a set of directories taken from the
* configuration file
*/
$loader = new Loader();
$loader->registerNamespaces($config->namespaces->toArray());
$loader->register();
require K_PATH . '/vendor/autoload.php';
/**
* LOGGER
*
* The essential logging service
*/
$format = '[%date%][%type%] %message%';
$name = K_PATH . '/var/log/' . date('Y-m-d') . '-kitsune.log';
$logger = new LoggerFile($name);
$formatter = new LoggerFormatter($format);
$logger->setFormatter($formatter);
$di->set('logger', $logger, true);
/**
* ERROR HANDLING
*/
ini_set('display_errors', boolval(K_DEBUG));
error_reporting(E_ALL);
set_error_handler(function ($exception) use($logger) {
if ($exception instanceof \Exception) {
$logger->error($exception->__toString());
} else {
$logger->error(json_encode(debug_backtrace()));
}
});
set_exception_handler(function (\Exception $exception) use($logger) {
$logger->error($exception->getMessage());
});
register_shutdown_function(function () use($logger, $memoryUsage, $currentTime) {
$memoryUsed = number_format((memory_get_usage() - $memoryUsage) / 1024, 3);
$executionTime = number_format(microtime(true) - $currentTime, 4);
//.........这里部分代码省略.........
示例11: Flash
return new Flash(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning'));
});
/**
* Start the session the first time some component request the session service
*/
$di->setShared('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
/*
* Routes
*/
#$di->set('router', function(){
# require 'routes.php';
# return $router;
#});
/**
* Logger service
*/
$di->set('logger', function ($filename = null, $format = null) use($config) {
$format = $format ?: $config->get('logger')->format;
$filename = trim($filename ?: $config->get('logger')->filename, '\\/');
$path = rtrim($config->get('logger')->path, '\\/') . DIRECTORY_SEPARATOR;
#$path = "/temp/system.log";
$formatter = new FormatterLine($format, $config->get('logger')->date);
$logger = new FileLogger($path . $filename);
$logger->setFormatter($formatter);
$logger->setLogLevel($config->get('logger')->logLevel);
return $logger;
});
示例12: run
public function run(DiInterface $diContainer, array $options = [])
{
$memoryUsage = memory_get_usage();
$currentTime = microtime(true);
$this->diContainer = $diContainer;
/**
* The app path
*/
if (!defined('K_PATH')) {
define('K_PATH', dirname(dirname(dirname(__FILE__))));
}
/**
* We will need the Utils class
*/
require_once K_PATH . '/library/Kitsune/Utils.php';
/**
* Utils class
*/
$utils = new Utils();
$this->diContainer->set('utils', $utils, true);
/**
* Check if this is a CLI app or not
*/
$cli = $utils->fetch($options, 'cli', false);
if (!defined('K_CLI')) {
define('K_CLI', $cli);
}
$tests = $utils->fetch($options, 'tests', false);
if (!defined('K_TESTS')) {
define('K_TESTS', $tests);
}
/**********************************************************************
* CONFIG
**********************************************************************/
/**
* The configuration is split into two different files. The first one
* is the base configuration. The second one is machine/installation
* specific.
*/
if (!file_exists(K_PATH . '/var/config/base.php')) {
throw new \Exception('Base configuration files are missing');
}
if (!file_exists(K_PATH . '/var/config/config.php')) {
throw new \Exception('Configuration files are missing');
}
/**
* Get the config files and merge them
*/
$base = (require K_PATH . '/var/config/base.php');
$specific = (require K_PATH . '/var/config/config.php');
$combined = array_replace_recursive($base, $specific);
$config = new Config($combined);
$this->diContainer->set('config', $config, true);
/**
* Check if we are in debug/dev mode
*/
if (!defined('K_DEBUG')) {
$debugMode = boolval($utils->fetch($config, 'debugMode', false));
define('K_DEBUG', $debugMode);
}
/**
* Access to the debug/dev helper functions
*/
if (K_DEBUG) {
require_once K_PATH . '/library/Kitsune/Debug.php';
}
/**********************************************************************
* LOADER
**********************************************************************/
/**
* We're a registering a set of directories taken from the
* configuration file
*/
$loader = new Loader();
$loader->registerNamespaces($config->namespaces->toArray());
$loader->register();
require K_PATH . '/vendor/autoload.php';
/**********************************************************************
* LOGGER
**********************************************************************/
/**
* The essential logging service
*/
$format = '[%date%][%type%] %message%';
$name = K_PATH . '/var/log/' . date('Y-m-d') . '-kitsune.log';
$logger = new LoggerFile($name);
$formatter = new LoggerFormatter($format);
$logger->setFormatter($formatter);
$this->diContainer->set('logger', $logger, true);
/**********************************************************************
* ERROR HANDLING
**********************************************************************/
ini_set('display_errors', boolval(K_DEBUG));
error_reporting(E_ALL);
set_error_handler(function ($exception) use($logger) {
if ($exception instanceof \Exception) {
$logger->error($exception->__toString());
} else {
$logger->error(json_encode(debug_backtrace()));
}
//.........这里部分代码省略.........
示例13: initLogger
/**
* 日志处理
*/
protected function initLogger()
{
$config = $this->config;
$this->di['logger'] = function () use($config) {
$logLevel = $this->debug ? Logger::DEBUG : Logger::ERROR;
if ($config->offsetExists('logger')) {
try {
if ($config->logger->offsetExists('path') == false) {
throw new \Exception('logger path not in config.');
}
$path = $config->logger->path;
$path = str_replace('{{date}}', date("Ymd"), $path);
if ($config->logger->offsetExists('formatter')) {
$formatter = new LineFormatter($config->logger->formatter);
} else {
$formatter = new LineFormatter('%date%[%type%] - %message%');
}
$logger = new LoggerFile($path);
$logger->setFormatter($formatter);
$logger->setLogLevel($logLevel);
return $logger;
} catch (\Exception $e) {
}
}
$logger = new LoggerStream("php://stderr");
$logger->setLogLevel($logLevel);
return $logger;
};
}
示例14: initLogger
/**
* Initialize the Logger.
*/
protected function initLogger()
{
ErrorHandler::register();
$this->di->set('logger', function ($filename = null, $format = null) {
/** @var DiInterface $this */
$config = $this->getShared('config');
$format = $format ?: $config->get('logger')->format;
$filename = trim($filename ?: $config->get('logger')->filename, '\\/');
$path = rtrim($config->get('logger')->path, '\\/') . DIRECTORY_SEPARATOR;
if (false === strpos($filename, '.log')) {
$filename = "{$filename}.log";
}
$formatter = new FormatterLine($format, $config->get('logger')->date);
$logger = new FileLogger($path . $filename);
$logger->setFormatter($formatter);
$logger->setLogLevel($config->get('logger')->logLevel);
return $logger;
});
}
示例15: initLogger
/**
*
* @param type $options
*/
protected function initLogger($options = [])
{
$config = $this->_di->get('config');
$this->_di->setShared('logger', function () use($config) {
if (!file_exists($config->logger->file)) {
mkdir($config->logger->file, 0777, true);
}
$logger = new LoggerFile($config->logger->file . date('Y-m-d') . '.log');
$formatter = new LoggerFormatter($config->logger->format);
$logger->setFormatter($formatter);
return $logger;
});
$this->_di->setShared('loggerDb', function () use($config) {
if (!file_exists($config->logger->file . '/db/')) {
mkdir($config->logger->file . '/db/', 0777, true);
}
$logger = new LoggerFile($config->logger->file . '/db/' . date('Y-m-d') . '.log');
$formatter = new LoggerFormatter($config->logger->format);
$logger->setFormatter($formatter);
return $logger;
});
}