LaraflareDocsPackages13.x

Docs / The Basics / Logging

Logging

Introduction

To help you learn more about what's happening within your application, Laraflare provides robust logging services that allow you to log messages to files and to the console, where Cloudflare collects them when your application runs on Workers.

Laraflare logging is based on "channels". Each channel represents a specific way of writing log information. For example, the single channel writes log files to a single log file, while the stderr channel writes each message to the process's error output. Log messages may be written to multiple channels based on their severity.

Configuration

All of the configuration options that control your application's logging behavior are housed in the config/logging.ts configuration file. This file allows you to configure your application's log channels, so be sure to review each of the available channels and their options. We'll review a few common options below.

By default, Laraflare will use the stack channel when logging messages. The stack channel is used to aggregate multiple log channels into a single channel. For more information on building stacks, check out the documentation below.

Available Channel Drivers

Each log channel is powered by a "driver". The driver determines how and where the log message is actually recorded. The following log channel drivers are available in every Laraflare application. An entry for most of these drivers is already present in your application's config/logging.ts configuration file, so be sure to review this file to become familiar with its contents:

Name Description
daily A single file per day, keeping the newest max_files of them.
null Discards every message.
single A single file or path based logger channel.
stack A wrapper to facilitate creating "multi-channel" channels.
stderr Writes each message to the error output, as a line of text or as a line of JSON.

Note

Workers have no disk to write files to. On Workers the single and daily channels write to the console like stderr does, as JSON, which is what Cloudflare's logs show. Slack, syslog, Papertrail and Monolog handlers are not available.

Configuring the Channel Name

Each message carries a "channel name" that matches the current environment, such as production or local.

Channel Prerequisites

Configuring the Single and Daily Channels

The single and daily channels write to the file named by their path option (storage/logs/app.log by default). The daily channel adds the date to the file name, such as app-2026-11-21.log, and its retention policy is configured via the max_files configuration option. The LOG_DAILY_DAYS environment variable may also be used to configure retention for the daily channel.

Configuring the Stderr Channel

The stderr channel has a formatter option, either line or json. The default config/logging.ts uses json on Workers and line everywhere else.

Logging Deprecation Warnings

You may specify your preferred deprecations log channel using the LOG_DEPRECATIONS_CHANNEL environment variable, or within your application's config/logging.ts configuration file:

deprecations: {
  channel: envString('LOG_DEPRECATIONS_CHANNEL', 'null'),
  trace: envBoolean('LOG_DEPRECATIONS_TRACE', false),
},

channels: {
  // ...
},

Building Log Stacks

As mentioned previously, the stack driver allows you to combine multiple channels into a single log channel for convenience. To illustrate how to use log stacks, let's take a look at an example configuration that you might see in a production application:

channels: {
  stack: {
    driver: 'stack',
    channels: ['daily', 'stderr'],
    ignore_exceptions: false,
  },

  daily: {
    driver: 'daily',
    path: 'storage/logs/app.log',
    level: envString('LOG_LEVEL', 'debug'),
    max_files: 14,
    replace_placeholders: true,
  },

  stderr: {
    driver: 'stderr',
    level: envString('LOG_LEVEL', 'error'),
    formatter: 'line',
  },
},

Let's dissect this configuration. First, notice our stack channel aggregates two other channels via its channels option: daily and stderr. So, when logging messages, both of these channels will have the opportunity to log the message. However, as we will see below, whether these channels actually log the message may be determined by the message's severity / "level".

Log Levels

Take note of the level configuration option present on the daily and stderr channel configurations in the example above. This option determines the minimum "level" a message must be in order to be logged by the channel. Laraflare offers all of the log levels defined in the RFC 5424 specification. In descending order of severity, these log levels are: emergency, alert, critical, error, warning, notice, info, and debug.

So, imagine we log a message using the debug method:

Log.debug('An informational message.');

Given our configuration, the daily channel will write the message to its file; however, since the message is not error or above, it will not be written by the stderr channel. However, if we log an emergency message, it will be written by both channels since the emergency level is above our minimum level threshold for both channels:

Log.emergency('The system is down!');

Writing Log Messages

You may write information to the logs using the Log facade. As previously mentioned, the logger provides the eight logging levels defined in the RFC 5424 specification: emergency, alert, critical, error, warning, notice, info and debug:

import { Log } from '@laraflare/framework/log';

Log.emergency(message);
Log.alert(message);
Log.critical(message);
Log.error(message);
Log.warning(message);
Log.notice(message);
Log.info(message);
Log.debug(message);

You may call any of these methods to log a message for the corresponding level. By default, the message will be written to the default log channel as configured by your logging configuration file:

import { Log } from '@laraflare/framework/log';
import { response } from '@laraflare/framework';
import { User } from '../Models/User';

export class UserController {
  /**
   * Show the profile for the given user.
   */
  async show(id: string) {
    Log.info('Showing the user profile for user: {id}', { id });

    return response().json(await User.findOrFail(id));
  }
}

Contextual Information

An object of contextual data may be passed to the log methods. This contextual data will be formatted and displayed with the log message:

import { Log } from '@laraflare/framework/log';

Log.info('User {id} failed to login.', { id: user.id });

Occasionally, you may wish to specify some contextual information that should be included with all subsequent log entries. For example, you may wish to log a request ID that is associated with each incoming request to your application. To accomplish this, you may call the Log facade's withContext method:

import { randomUUID } from 'node:crypto';
import { Log } from '@laraflare/framework/log';
import type { Request } from '@laraflare/framework';

export class AssignRequestId {
  /**
   * Handle an incoming request.
   */
  async handle(request: Request, next: (request: Request) => Promise<Response>): Promise<Response> {
    const requestId = randomUUID();

    Log.withContext({
      'request-id': requestId,
    });

    const response = await next(request);

    response.headers.set('Request-Id', requestId);

    return response;
  }
}

The context is kept for the request or job being handled, so one request's context never shows up in another's. To run a piece of code with its own context, pass it to Log.scope, and to forget context, call withoutContext with the keys to forget, or with none to forget all of it.

Note

The request's context is also written to every entry as its extra data.

Writing to Specific Channels

Sometimes you may wish to log a message to a channel other than your application's default channel. You may use the channel method on the Log facade to retrieve and log to any channel defined in your configuration file:

import { Log } from '@laraflare/framework/log';

Log.channel('stderr').info('Something happened!');

If you would like to create an on-demand logging stack consisting of multiple channels, you may use the stack method:

Log.stack(['single', 'stderr']).info('Something happened!');

Tailing Log Messages

Often you may need to tail your application's logs in real time. Locally, the single and daily channels write to files under storage/logs, which you may follow with your terminal's tail -f. On Workers, there is no log file; follow the console output of a deployed Worker with npx wrangler tail.

To be told about each message as it is written, register a listener with Log.listen, which returns a function that removes it:

const stop = Log.listen((entry) => {
  console.log(entry.level, entry.message);
});
Adapted from Laravel's documentation, Copyright (c) Taylor Otwell, used under the MIT License.