Log::Dispatchouli(3) User Contributed Perl Documentation Log::Dispatchouli(3)

# [SYNOPSIS](#SYNOPSIS)

```
  my $logger = Log::Dispatchouli->new({
    ident     => 'stuff-purger',
    facility  => 'daemon',
    to_stdout => $opt->{print},
    debug     => $opt->{verbose}
  });
  $logger->log([ "There are %s items left to purge...", $stuff_left ]);
  $logger->log_debug("this is extra often-ignored debugging log");
  $logger->log_fatal("Now we will die!!");
```

# [DESCRIPTION](#DESCRIPTION)

Log::Dispatchouli is a thin layer above Log::Dispatch and meant to make it dead simple to add logging to a program without having to think much about categories, facilities, levels, or things like that. It is meant to make logging just configurable enough that you can find the logs you want and just easy enough that you will actually log things.

Log::Dispatchouli can log to syslog (if you specify a facility), standard error or standard output, to a file, or to an array in memory. That last one is mostly useful for testing.

In addition to providing as simple a way to get a handle for logging operations, Log::Dispatchouli uses String::Flogger to process the things to be logged, meaning you can easily log data structures. Basically: strings are logged as is, arrayrefs are taken as (sprintf format, args), and subroutines are called only if needed. For more information read the String::Flogger docs.

# [LOGGER PREFIX](#LOGGER_PREFIX)

Log messages may be prepended with information to set context. This can be set at a logger level or per log item. The simplest example is:

```
  my $logger = Log::Dispatchouli->new( ... );
  $logger->set_prefix("Batch 123: ");
  $logger->log("begun processing");
  # ...
  $logger->log("finished processing");
```

The above will log something like:

```
  Batch 123: begun processing
  Batch 123: finished processing
```

To pass a prefix per-message:

```
  $logger->log({ prefix => 'Sub-Item 234: ' }, 'error!')
  # Logs: Batch 123: Sub-Item 234: error!
```

If the prefix is a string, it is prepended to each line of the message. If it is a coderef, it is called and passed the message to be logged. The return value is logged instead.

Proxy loggers also have their own prefix settings, which accumulate. So:

```
  my $proxy = $logger->proxy({ proxy_prefix => 'Subsystem 12: ' });
  $proxy->set_prefix('Page 9: ');
  $proxy->log({ prefix => 'Paragraph 6: ' }, 'Done.');
```

...will log...

```
  Batch 123: Subsystem 12: Page 9: Paragraph 6: Done.
```

```
  my $logger = Log::Dispatchouli->new(\%arg);
```

This returns a new logger, a Log::Dispatchouli object.

Valid arguments are:

```
  ident       - the name of the thing logging (mandatory)
  to_self     - log to the logger object for testing; default: false
  to_stdout   - log to STDOUT; default: false
  to_stderr   - log to STDERR; default: false
  facility    - to which syslog facility to send logs; default: none
  to_file     - DEPRECATED: this option will be removed in 2025
                log to PROGRAM_NAME.YYYYMMDD in the log path; default: false
  log_file    - a leaf name for the file to log to with to_file
  log_path    - path in which to log to file; defaults to DISPATCHOULI_PATH
                environment variable or, failing that, to your system's tmpdir
  file_format - this optional coderef is passed the message to be logged
                and returns the text to write out
  log_pid     - if 1, prefix all log entries with the pid; default: true
                can also be a comma-delimited list of log targets where pid is
                logged, like "stderr,syslog"; mostly useful for logging pid in
                syslog, but not on standard I/O
  fail_fatal  - a boolean; if true, failure to log is fatal; default: true
  muted       - a boolean; if true, only fatals are logged; default: false
  debug       - a boolean; if true, log_debug method is not a no-op
                defaults to the truth of the DISPATCHOULI_DEBUG env var
  quiet_fatal - 'stderr' or 'stdout' or an arrayref of zero, one, or both
                fatal log messages will not be logged to these
                (default: stderr)
  config_id   - a name for this logger's config; rarely needed!
  syslog_socket - a value for Sys::Syslog's "socket" arg; default: "native"
```

The log path is either */tmp* or the value of the *DISPATCHOULI\_PATH* env var.

If the *DISPATCHOULI\_NOSYSLOG* env var is true, we don't log to syslog.

These methods turn on logging to STDOUT or STDERR, respectively. If that logging has already been enabled, these methods do nothing.

There is not, yet, a *disable* version of these methods.

```
  $logger->log(@messages);
  $logger->log(\%arg, @messages);
```

This method uses String::Flogger on the input, then *unconditionally* logs the result. Each message is flogged individually, then joined with spaces.

If the first argument is a hashref, it will be used as extra arguments to logging. It may include a "prefix" entry to preprocess the message by prepending a string (if the prefix is a string) or calling a subroutine to generate a new message (if the prefix is a coderef).

This behaves like the "log" method, but will throw the logged string as an exception after logging.

This method can also be called as "fatal", to match other popular logging interfaces. **If you want to override this method, you must override** **"log\_fatal" and not "fatal"**.

This behaves like the "log" method, but will only log (at the debug level) if the logger object has its debug property set to true.

This method can also be called as "debug", to match other popular logging interfaces. **If you want to override this method, you must override** **"log\_debug" and not "debug"**.

This method is like "log", but is used for structured logging instead of free form text. It's invoked like this:

```
  $logger->log($event_type => $data_ref);
```

$event\_type should be a simple string, probably a valid identifier, that identifies the kind of event being logged. It is suggested, but not required, that all events of the same type have the same kind of structured data in them.

$data\_ref is a set of key/value pairs of data to log in this event. It can be an arrayref (in which case the ordering of pairs is preserved) or a hashref (in which case they are sorted by key).

The logged string will be in logfmt format, meaning a series of key=value pairs separated by spaces and following these rules:

When values are undef, they are represented as "~".

When values are array references, the index/values are mapped over, so that:

```
  key => [ 'a', 'b' ]
```

becomes

```
  key.0=a key.1=b
```

When values are hash references, the key/values are mapped, with keys sorted, so that:

```
  key => { b => 2, a => 1 }
```

becomes

```
  key.a=1 key.b=2
```

This expansion is performed recursively. If a value itself recurses, appearances of a reference after the first time will be replaced with a string like "&foo.bar", pointing to the first occurrence. *This is not meant to be* *a robust serialization mechanism.* It's just here to help you be a little lazy. Don't push the limits.

If the value in $data\_ref is a code reference, it will be called and its result logged. If its result is also a code reference, you get whatever garbage that code reference stringifies to.

If the value in $data\_ref is a reference reference, then the referenced scalar will be passed to String::Flogger, and the resulting string will be used as the value to log. That string will be quoted as described above, if needed.

This method is just like "log\_event", but will log nothing unless the logger has its "debug" property set to true.

```
  $logger->set_debug($bool);
```

This sets the logger's debug property, which affects the behavior of "log\_debug".

This gets the logger's debug property, which affects the behavior of "log\_debug".

This method does nothing, and is only useful for Log::Dispatchouli::Proxy objects. See Methods for Proxy Loggers, below.

```
  $logger->set_muted($bool);
```

This sets the logger's muted property, which affects the behavior of "log".

This gets the logger's muted property, which affects the behavior of "log".

This method does nothing, and is only useful for Log::Dispatchouli::Proxy objects. See Methods for Proxy Loggers, below.

```
  my $prefix = $logger->get_prefix;
```

This method returns the currently-set prefix for the logger, which may be a string or code reference or undef. See Logger Prefix.

```
  $logger->set_prefix( $new_prefix );
```

This method changes the prefix. See Logger Prefix.

This method clears any set logger prefix. (It can also be called as "unset\_prefix", but this is deprecated. See Logger Prefix.

This method returns the logger's ident.

This method returns the logger's configuration id, which defaults to its ident. This can be used to make two loggers equivalent in Log::Dispatchouli::Global so that trying to reinitialize with a new logger with the same "config\_id" as the current logger will not throw an exception, and will simply do no thing.

# [METHODS FOR SUBCLASSING](#METHODS_FOR_SUBCLASSING)

## [string\_flogger](#string_flogger)

This method returns the thing on which *flog* will be called to format log messages. By default, it just returns "String::Flogger"

## [env\_prefix](#env_prefix)

This method should return a string used as a prefix to find environment variables that affect the logger's behavior. For example, if this method returns "XYZZY" then when checking the environment for a default value for the "debug" parameter, Log::Dispatchouli will first check "XYZZY\_DEBUG", then "DISPATCHOULI\_DEBUG".

By default, this method returns "()", which means no extra environment variable is checked.

## [env\_value](#env_value)

```
  my $value = $logger->env_value('DEBUG');
```

This method returns the value for the environment variable suffix given. For example, the example given, calling with "DEBUG" will check "DISPATCHOULI\_DEBUG".

```
  my $str = $logger->flog_messages($m1, $m2, ...);
```

This returns the string that would have been logged if the given arguments had been passed to "$logger-&gt;log(...)", without regard for log level, debugging, or the like.

Unlike using the logger's string flogger, this will include any relevant prefix strings.

```
  my $str = $logger->fmt_event($event_type => $data_ref);
```

This method is equivalent to "flog\_messages", but for an event. It returns the string format of the event, including all relevant prefixes.

# [METHODS FOR TESTING](#METHODS_FOR_TESTING)

## [new\_tester](#new_tester)

```
  my $logger = Log::Dispatchouli->new_tester( \%arg );
```

This returns a new logger that logs only "to\_self". It's useful in testing. If no "ident" arg is provided, one will be generated. "log\_pid" is off by default, but can be overridden.

"\\%arg" is optional.

## [events](#events)

This method returns the arrayref of events logged to an array in memory (in the logger). If the logger is not logging "to\_self" this raises an exception.

## [clear\_events](#clear_events)

This method empties the current sequence of events logged into an array in memory. If the logger is not logging "to\_self" this raises an exception.

# [METHODS FOR PROXY LOGGERS](#METHODS_FOR_PROXY_LOGGERS)

## [proxy](#proxy)

```
  my $proxy_logger = $logger->proxy( \%arg );
```

This method returns a new proxy logger -- an instance of Log::Dispatchouli::Proxy -- which will log through the given logger, but which may have some settings localized.

%arg is optional. It may contain the following entries:

## [parent](#parent)

## [logger](#logger)

These methods return the logger itself. (They're more useful when called on proxy loggers.)

This returns the underlying Log::Dispatch object. This is not the method you're looking for. Move along.

This method is an experimental feature to allow you to pick an alternate dispatch class for stderr and stdio. By default, Log::Dispatch::Screen is used. **This feature may go away at any time.**

# [METHODS FOR API COMPATIBILITY](#METHODS_FOR_API_COMPATIBILITY)

To provide compatibility with some other loggers, most specifically Log::Contextual, the following methods are provided. You should not use these methods without a good reason, and you should never subclass them. Instead, subclass the methods they call.

# [SEE ALSO](#SEE_ALSO)

# [POD ERRORS](#POD_ERRORS)

Hey! **The above document had some coding errors, which are explained below:**

[Around line 99:](#Around)

Unknown directive: =method

[Around line 265:](#Around~2)

Unknown directive: =method

[Around line 267:](#Around~3)

Unknown directive: =method

[Around line 318:](#Around~4)

Unknown directive: =method

[Around line 383:](#Around~5)

Unknown directive: =method

[Around line 403:](#Around~6)

Unknown directive: =method

[Around line 424:](#Around~7)

Unknown directive: =method

[Around line 528:](#Around~8)

Unknown directive: =method

[Around line 541:](#Around~9)

Unknown directive: =method

[Around line 552:](#Around~10)

Unknown directive: =method

[Around line 561:](#Around~11)

Unknown directive: =method

[Around line 573:](#Around~12)

Unknown directive: =method

[Around line 586:](#Around~13)

Unknown directive: =method

[Around line 595:](#Around~14)

Unknown directive: =method

[Around line 604:](#Around~15)

Unknown directive: =method

[Around line 611:](#Around~16)

Unknown directive: =method

[Around line 617:](#Around~17)

Unknown directive: =method

[Around line 629:](#Around~18)

Unknown directive: =method

[Around line 637:](#Around~19)

Unknown directive: =method

[Around line 695:](#Around~20)

Unknown directive: =method

[Around line 706:](#Around~21)

Unknown directive: =method

[Around line 832:](#Around~22)

Unknown directive: =method

[Around line 841:](#Around~23)

Unknown directive: =method

2026-10-25 perl v5.44.0

---

Document: Log::Dispatchouli(3pm) from perl-log-dispatchouli 3.101-r3 (Chainguard OS)
Source: https://github.com/rjbs/Log-Dispatchouli
