lesage
lesage 530 XP
asked

Customisation des Logs dans Laravel 11

Bonjour, j'utilise Laravel 11, j'essaie d'implémenté une arborescence de logs pour mon aplli Laravel similaire à ceci : storage/logs/2024/05/09/ophtalmo_11-2024-05-09.log

Voici le chanel des logs que j'ai rajouté dans config/logging.php :

1'custom' => [
2 'driver' => 'single',
3 'path' => storage_path('logs/' . date('Y/m/d') . '/ophtalmo_' . date('H') . '-' . date('Y-m-d') . '.log'),
4 'level' => 'debug',
5 ],
1'custom' => [
2 'driver' => 'single',
3 'path' => storage_path('logs/' . date('Y/m/d') . '/ophtalmo_' . date('H') . '-' . date('Y-m-d') . '.log'),
4 'level' => 'debug',
5 ],

mon .env :

1LOG_CHANNEL=custom
1LOG_CHANNEL=custom

J'ai creer un provider LogServiceProvider.php :

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\Facades\File;
6use Illuminate\Support\ServiceProvider;
7 
8class LogServiceProvider extends ServiceProvider
9{
10 /**
11 * Register services.
12 */
13 public function register(): void
14 {
15 //
16 }
17 
18 /**
19 * Bootstrap services.
20 */
21 public function boot(): void
22 {
23 $logPath = storage_path('logs/' . date('Y/m/d'));
24 if (!File::exists($logPath)) {
25 File::makeDirectory($logPath, 0755, true);
26 }
27 }
28}
1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\Facades\File;
6use Illuminate\Support\ServiceProvider;
7 
8class LogServiceProvider extends ServiceProvider
9{
10 /**
11 * Register services.
12 */
13 public function register(): void
14 {
15 //
16 }
17 
18 /**
19 * Bootstrap services.
20 */
21 public function boot(): void
22 {
23 $logPath = storage_path('logs/' . date('Y/m/d'));
24 if (!File::exists($logPath)) {
25 File::makeDirectory($logPath, 0755, true);
26 }
27 }
28}

le boostrap/providers.php :

1<?php
2 
3return [
4 App\Providers\AppServiceProvider::class,
5 App\Providers\LogServiceProvider::class,
6];
1<?php
2 
3return [
4 App\Providers\AppServiceProvider::class,
5 App\Providers\LogServiceProvider::class,
6];
lesage
lesage 530 XP
posted

Merci, ça marches très bien,

Confirm deletion

Are you sure you want to delete this reply? This action is irreversible.

russeloken
posted
Best answer

Bonjour chef Voici ce que je te propose crée une classe CustomLogger.php

je precise que pour moi l'approche avec une class Log personnalisée me semble plus propre

Dans ton fichier config/logging.php

si tu as une interrogation concernant le le faite que 'driver' => 'monolog' voici le lien de la doc

Tu maintiens ton LOG_CHANNEL=custom A mon avis pas besoin d'un log provider comme tu le fais

je pense qu'avec ces etapes tu pourras gerer ton probleme.

Confirm deletion

Are you sure you want to delete this reply? This action is irreversible.

You need Log in or Create an account to join the conversation.

Confirm deletion

Are you sure you want to delete this thread? This action is irreversible.