Better handling of log file failures (#9539)

Fix issues when APP_LOG is empty, unwritable , or pointed at a directory.
APP_LOG takes precedence over $config['log_file'] now.

DO NOT DELETE THIS TEXT

#### Please note

> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.

- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)

#### Testers

If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`.  If there are schema changes, you can ask on discord how to revert.
This commit is contained in:
Tony Murray
2018-12-16 15:26:20 -06:00
committed by Neil Lathwood
parent 74882e3950
commit 0b724c3fbe
7 changed files with 21 additions and 10 deletions

View File

@@ -220,6 +220,18 @@ class Checks
$commands[] = "usermod -a -G $group $current_user";
}
// check for invalid log setting
$log_file = config('app.log') ?: Config::get('log_file', base_path('logs/librenms.log'));
if (!is_file($log_file) || !is_writable($log_file)) {
// override for proper error output
$dirs = [$log_file];
$install_dir = $log_file;
$commands = [
'<h3>Cannot write to log file: &quot;' . $log_file . '&quot;</h3>',
'Make sure it exists and is writable, or change your LOG_DIR setting.'
];
}
// selinux:
$commands[] = '<h4>If using SELinux you may also need:</h4>';
foreach ($dirs as $dir) {
@@ -230,6 +242,7 @@ class Checks
// use pre-compiled template because we probably can't compile it.
$template = file_get_contents(base_path('resources/views/errors/static/file_permissions.html'));
$content = str_replace('!!!!CONTENT!!!!', '<p>' . implode('</p><p>', $commands) . '</p>', $template);
$content = str_replace('!!!!LOG_FILE!!!!', $log_file, $content);
return SymfonyResponse::create($content);
}

View File

@@ -18,7 +18,6 @@ class AppServiceProvider extends ServiceProvider
* Bootstrap any application services.
*
* @return void
* @throws DatabaseConnectException caught by App\Exceptions\Handler and displayed to the user
*/
public function boot()
{
@@ -28,9 +27,9 @@ class AppServiceProvider extends ServiceProvider
// load config
Config::load();
// direct log output to librenms.log
// replace early boot logging redirect log to config location, unless APP_LOG is set
Log::getMonolog()->popHandler(); // remove existing errorlog logger
Log::useFiles(Config::get('log_file', base_path('logs/librenms.log')), 'error');
Log::useFiles(config('app.log') ?: Config::get('log_file', base_path('logs/librenms.log')), 'error');
// Blade directives (Yucky because of < L5.5)
Blade::directive('config', function ($key) {

View File

@@ -42,8 +42,8 @@ $app->singleton(
);
$app->configureMonologUsing(function (Monolog\Logger $logger) use ($app) {
$path = $app->basePath(config('app.log'));
$logger->pushHandler(new \Monolog\Handler\StreamHandler($path));
$path = config('app.log') ?: $app->basePath('logs/librenms.log');
$logger->pushHandler(new \Monolog\Handler\StreamHandler($path, \Monolog\Logger::toMonologLevel(config('app.log_level', 'debug'))));
});
/*

View File

@@ -120,9 +120,9 @@ return [
|
*/
'log' => env('APP_LOG', 'logs/librenms.log'), // log to the default file, until boot
'log' => env('APP_LOG'), // log file to write to
'log_level' => env('APP_LOG_LEVEL', 'debug'),
'log_level' => env('APP_LOG_LEVEL', 'error'),
/*
|--------------------------------------------------------------------------

View File

@@ -10,7 +10,4 @@
@foreach($commands as $command)
<p>{{ $command }}</p>
@endforeach
<hr class="separator"/>
<p>@lang("If that doesn't fix the issue. You can find how to get help at") <a href="https://docs.librenms.org/Support">https://docs.librenms.org/Support</a>.</p>
@endsection

View File

@@ -70,6 +70,7 @@
!!!!CONTENT!!!!
<hr class="separator"/>
<p>Check your log for more details. (<i>!!!!LOG_FILE!!!!</i>)</p>
<p>If that doesn't fix the issue. You can find how to get help at <a href="https://docs.librenms.org/Support">https://docs.librenms.org/Support</a>.</p>
</div>

View File

@@ -68,6 +68,7 @@
@yield('content')
<hr class="separator"/>
<p>@lang("Check your log for more details.") ({{ $log_file ?: 'librenms.log' }})</p>
<p>@lang("If you need additional help, you can find how to get help at") <a href="https://docs.librenms.org/Support">https://docs.librenms.org/Support</a>.</p>
</div>