Skip to content

Instantly share code, notes, and snippets.

@adamgoucher
Created August 11, 2018 00:30
Show Gist options
  • Save adamgoucher/499ecfcfd5c8b7801e0e827ed43df386 to your computer and use it in GitHub Desktop.
Save adamgoucher/499ecfcfd5c8b7801e0e827ed43df386 to your computer and use it in GitHub Desktop.
How to create simple structured logs for your Laravel 5.x app
Adams-MacBook-Pro:tether adamgoucher$ git diff bootstrap/app.php
diff --git a/bootstrap/app.php b/bootstrap/app.php
index 36a1fa6a9..d9958cd28 100755
--- a/bootstrap/app.php
+++ b/bootstrap/app.php
@@ -1,5 +1,8 @@
<?php
+use Monolog\Formatter\JsonFormatter;
+use Monolog\Handler\RotatingFileHandler;
+
/*
|--------------------------------------------------------------------------
| Create The Application
@@ -41,6 +44,24 @@ $app->singleton(
'App\Exceptions\Handler'
);
+/*
+|--------------------------------------------------------------------------
+| Logging Changes
+|--------------------------------------------------------------------------
+|
+| Structured logs ftw?
+|
+*/
+$app->configureMonologUsing(function ($monolog) {
+ $path = storage_path() . '/logs/laravel.log';
+ $days = config('app.log_max_files', 5);
+ $handler = new RotatingFileHandler($path, $days);
+ $handler->setFormatter(new JsonFormatter());
+
+ $monolog->pushHandler($handler);
+});
+
+
/*
|--------------------------------------------------------------------------
| Return The Application
Adams-MacBook-Pro:tether adamgoucher$ tail storage/logs/laravel-2018-08-11.log
{"message":"bar","context":[],"level":100,"level_name":"DEBUG","channel":"development","datetime":{"date":"2018-08-11 00:27:34.839951","timezone_type":3,"timezone":"UTC"},"extra":[]}
Adams-MacBook-Pro:tether adamgoucher$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment