根据您提供的信息,我无法真正回答您的问题,但今天是新闻缓慢的一天,因此整理了一个可正常工作的 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! [] []
我注意到的一件事是,如果日志文件的路径是错误的,那么 Monolog 似乎会吞下它(这并不完全理想)。这很可能是您的问题。
不管怎样,抓住上面的代码并搞砸它。希望您能够解决您和我之间的差异,并让您的工作正常。