Logging in Laravel

Tram Ho

Introduce

  • To help you know what’s going on in the system, Laravel provides powerful logging services that allow you to log files, system logs or even Slack to notify the entire team. your
  • Not only to help you debug, but it also helps you to record the execution history as desired
  • Laravel uses the Monolog library to provide and support many different ways of handling logs. You can also reconfigure or combine them to handle the log as you like

1. Configuration and types of channel drivers

All log configuration is in config/logging.php file By default Laravel will use stack channel to save log

  • Channel drivers There are many types of log channels that Laravel provides. Depending on the requirements of the project, you can use 1 or a combination of the following channel log types:
    Namedescription
    customDriver allows you to call a class factory to create a new channel
    dailyUse RotatingFileHandler class to log and log file will be generated daily
    errorlogUse ErrorLogHandler class to log and log files to php’s errorlog
    monologAs a factory driver that allows you to use any Monolog handle
    nullAs a driver that ignores all logs
    papertrailUse SyslogUdpHandler class to log and log will be stored in the cloud
    monologAs a factory driver that allows you to use any Monolog handle
    singleIs a driver that allows you to save logs to a single file
    slackUse SlackWebHookHandler to log to slack – a chat app
    stackAllows you to use multiple channels
    syslogUse SyslogHandler to log to the operating system’s system log

 

  • Channel Single and Daily : Single and daily channels alone have 3 more config options: buble , permission and locking
    NamedescriptionDefault
    bubbleAllow logging to other channels not for example when using stacktrue
    lockingLock the log file again before writing infalse
    permissionPermissions option for log file0644

 

  • Daily channel : has the option of days as the number of days the log file is kept, the default will be 7 days.
  • Papertrail channel : will need to configure the host and port of the cloud you use to log
  • Slack channel : will need url configuration, by default slack will only receive logs at critical level or higher
  • Deprecations log : To log warnings about unused features, use deprecations

image.png

  • Log level : emergency, alert, critical, error, warning, notice, info, and debug.

2. Specify another log channel

  • Using a log channel other than the default channel we use the Log facade to access and log into any defined channels:
  • To create the desired logging stack you use the stack method:
  • Create a desired channel by providing a configuration array to the build method:

3. Create a custom channel by creating a Factory class

  • First you need to create a factory class, which will return a Monolog instance. In this class you will need to define a method called __invoke , this method needs to pass a configuration array:
  • Then define your custom log channel in config/longging.php, using via to point to your factory class just created above
  • That you can use your custom channel
Share the news now

Source : Viblo