Log::Any::Adapter::Capture - Adapter for capturing log messages into an arrayref


version 1.714


  # temporarily redirect arrays of [ $level, $category, $message ] into an array
  Log::Any::Adapter->set( { lexically => \my $scope }, Capture => to => \my @array );

  # temporarily redirect just the text of log messages into an array
  Log::Any::Adapter->set( { lexically => \my $scope }, Capture => text => \my @array );

  # temporarily redirect the full argument list and context of each call, but only for
  # log levels 'info' and above.
    { lexically => \my $scope },
    Capture =>
        format => 'structured',
        to => \my @array,
        log_level => 'info'


This logging adapter provides a convenient way to capture log messages into a callback or arrayref of your choice without needing to write your own adapter. It is intended for cases where you want to temporarily capture log messages, such as showing them to a user of your application rather than having them written to a log file.



Specify a coderef or arrayref where the messages will be delivered. The content pushed onto the array or passed to the coderef depends on "format".


  sub ( $level, $category, $message_text ) { ... }
  push @to, [ $level, $category, $message_text ];

This is the default format. It passes/pushes 3 arguments: the name of the log level, the logging category, and the message text as a plain string.

  sub ( $message_text ) { ... }
  push @to, $message_text;

This format is the simplest, and only passes/pushes the text of the message.

  sub ( $level, $category, @message_parts, \%context? ) { ... }
  push @to, [ $level, $category, @message_parts, \%context? ];

This passes/pushes the full information available about the call to the logging method. The @message_parts are the actual arguments passed to the logging method, and if the final argument is a hashref, it is the combined context from the logging proxy and any overrides passed to the logging method.


Like other logging adapters, this optional argument can filter out any log messages above the specified threshhold. The default is to pass through all messages regardless of level.


These are not actual attributes, just shortcuts for others:


  text => $dest

is shorthand for

  format => 'text', to => $dest


  structured => $dest

is shorthand for

  format => 'structured', to => $dest


  • Jonathan Swartz <>

  • David Golden <>

  • Doug Bell <>

  • Daniel Pittman <>

  • Stephen Thirlwall <>


This software is copyright (c) 2017 by Jonathan Swartz, David Golden, and Doug Bell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.