根据文档,我尝试在我的Silex应用程序中使用Monolog
http://silex.sensiolabs.org/doc/master/providers/monolog.html我在app.php文件中声明MonologServiceProvider,如下所示:
use Silex\Provider\MonologServiceProvider;
$app->register(new MonologServiceProvider(), array(
'monolog.logfile' => __DIR__ . '/../files/logs/log.log',
));我试着在控制器上写下我的log.log文件:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// In a controler
$app['monolog']->info("test");
$app['monolog']->debug("test");
$app['monolog']->warning("test");
$app['monolog']->error("test");我没有任何错误,但它根本不工作。
我只是不想把我的“测试”消息放在我的log.log文件中,我该怎么做呢?
感谢你的帮助
发布于 2016-12-28 22:09:37
根据你给出的信息,我真的不能回答你的问题,但今天是一个慢消息的日子,所以我准备了一个工作日志的Silex站点。所有的文件都在Github上,但为了便于阅读,我将在这里重复它们。
composer.json
{
"require" : {
"silex/silex" : "^2.0",
"monolog/monolog" : "^1.0"
},
"autoload" : {
"psr-4" : {
"community\\" : "src/"
}
}
}public/index.php
<?php
use \community\app\Application;
require_once realpath(__DIR__ . '/../vendor/autoload.php');
$app = new Application();
$app["debug"] = true;
$app->run();src/app/Application.php
<?php
namespace community\app;
use \Silex\Application as SilexApplication;
use Silex\Provider\MonologServiceProvider;
class Application extends SilexApplication {
function __construct() {
parent::__construct();
$this->registerServices();
$this->mountControllers();
}
function registerServices(){
$this->register(new MonologServiceProvider(), [
"monolog.logfile" => realpath(__DIR__ . "/../../log") . "/general.log"
]);
}
function mountControllers() {
$this->get('/testLog', 'community\controller\TestLogController::doGet');
}
}src/controller/TestLogController.php
<?php
namespace community\controller;
use community\app\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class TestLogController {
public function doGet(Request $request, Application $app) {
$app["monolog"]->info("hi!");
return new Response("All good", Response::HTTP_OK);
}
}这将写入到log/general.log中,如下所示:
[2016-12-28 13:58:05] app.INFO: hi! [] []我注意到的一件事是,如果日志文件的路径是bung,那么Monolog似乎就会吞噬它(这并不是很理想)。这很可能是你的问题。
无论如何,抓起上面的代码,把它弄乱。希望你能找出你和我的不同之处,并让你的工作。
https://stackoverflow.com/questions/41322797
复制相似问题