From 0b724c3fbe488aac52ef682be02d09047b6cbd04 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Sun, 16 Dec 2018 15:26:20 -0600 Subject: [PATCH] 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 `, 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. --- app/Checks.php | 13 +++++++++++++ app/Providers/AppServiceProvider.php | 5 ++--- bootstrap/app.php | 4 ++-- config/app.php | 4 ++-- resources/views/errors/file_permissions.blade.php | 3 --- resources/views/errors/static/file_permissions.html | 1 + resources/views/layouts/error.blade.php | 1 + 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/Checks.php b/app/Checks.php index 479fb7534e..bad2b168ff 100644 --- a/app/Checks.php +++ b/app/Checks.php @@ -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 = [ + '

Cannot write to log file: "' . $log_file . '"

', + 'Make sure it exists and is writable, or change your LOG_DIR setting.' + ]; + } + // selinux: $commands[] = '

If using SELinux you may also need:

'; 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!!!!', '

' . implode('

', $commands) . '

', $template); + $content = str_replace('!!!!LOG_FILE!!!!', $log_file, $content); return SymfonyResponse::create($content); } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 544cf43b70..d53d77a165 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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) { diff --git a/bootstrap/app.php b/bootstrap/app.php index 5f7939de5e..4e90ab5014 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -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')))); }); /* diff --git a/config/app.php b/config/app.php index ae84631dbb..dff35c5dcd 100644 --- a/config/app.php +++ b/config/app.php @@ -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'), /* |-------------------------------------------------------------------------- diff --git a/resources/views/errors/file_permissions.blade.php b/resources/views/errors/file_permissions.blade.php index f3ac4730e3..78e77d6e28 100644 --- a/resources/views/errors/file_permissions.blade.php +++ b/resources/views/errors/file_permissions.blade.php @@ -10,7 +10,4 @@ @foreach($commands as $command)

{{ $command }}

@endforeach -
- -

@lang("If that doesn't fix the issue. You can find how to get help at") https://docs.librenms.org/Support.

@endsection diff --git a/resources/views/errors/static/file_permissions.html b/resources/views/errors/static/file_permissions.html index b6c1f207ab..444a03f407 100644 --- a/resources/views/errors/static/file_permissions.html +++ b/resources/views/errors/static/file_permissions.html @@ -70,6 +70,7 @@ !!!!CONTENT!!!!
+

Check your log for more details. (!!!!LOG_FILE!!!!)

If that doesn't fix the issue. You can find how to get help at https://docs.librenms.org/Support.

diff --git a/resources/views/layouts/error.blade.php b/resources/views/layouts/error.blade.php index afd926c11d..ccafa4e7f8 100644 --- a/resources/views/layouts/error.blade.php +++ b/resources/views/layouts/error.blade.php @@ -68,6 +68,7 @@ @yield('content')
+

@lang("Check your log for more details.") ({{ $log_file ?: 'librenms.log' }})

@lang("If you need additional help, you can find how to get help at") https://docs.librenms.org/Support.