<commit_before>use v6.c;

# Log::Any v 0.9.4 introduces extra-fields.
use Log::Any::Formatter:ver('0.9.4');

use Terminal::ANSIColor;

class Bailador::Log::Formatter is Log::Any::Formatter {
  has $.format = Nil;
  has $.template-format = Nil;
  has $!backend;
  has %.colors;
  has $.log-any-formatter;
  has $.colorize;

  use Log::Any::Formatter;
  submethod TWEAK {
    if $!template-format && $!format {
      die 'Only one of "template-format" or "format" should be specified.'
    }

    with $!template-format {
      when 'combined' {
        # Combined apache log format "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
        $!format = '\e{h} \e{l} \e{u} [\e{t}] "\e{r}" \e{s} \e{b} "\e{Referer}" \e{User-agent}';
      }
      when 'common' {
        # Common apache log format "%h %l %u %t \"%r\" %>s %b"
        $!format = '\e{h} \e{l} \e{u} [\e{t}] \e{r} \e{s} \e{b}';
      }
      when 'simple' {
        # "[%t] [%l] [pid %P] %F: %E: [client %a] %M"
        $!format = '[\e{t}] [\e{l}] [pid \e{P}] \e{F}: \e{E}: [client \e{a}] "\e{M}"';
      }
      when 'mpm' {
        #  [Fri Sep 09 10:42:29.902022 2011] [core:error] [pid 35708:tid 4328636416] [client 172.16.31.10] File does not exist: /usr/local/apache2/htdocs/favicon.ico
        # "[%{u}t] [%-m:%l] [pid %P:tid %T] %7F: %E: [client\ %a] %M% ,\ referer\ %{Referer}i"
        ...
      }
      default {
        die "Unknown value for 'template-format' ($!template-format)"
      }
    }

    with $!format {
      $!backend = Log::Any::FormatterBuiltIN.new(
        :format( $!format )
      );
    }
  }

  method format( :$date-time, :$msg!, :$category!, :$severity!, :%extra-fields ) {
    my %data;
    my $query-uri = %extra-fields<REQUEST_URI>
      ?? %extra-fields<REQUEST_URI>.split('?')[0]
      !! '';
    my $request =
        ( %extra-fields<REQUEST_METHOD> || '' ) ~ ' '
      ~ ( %extra-fields<PATH_INFO> || $query-uri ) ~ ' '
      ~ ( %extra-fields<SERVER_PROTOCOL> || '' );

    sub dateTime-to-Str( DateTime:D $date-time ) {
      my @month = <Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec>;<commit_msg>Add default formatter backend if no format for template-format is specified.<commit_after>use v6.c;

# Log::Any v 0.9.4 introduces extra-fields.
use Log::Any::Formatter:ver('0.9.4');

use Terminal::ANSIColor;

class Bailador::Log::Formatter is Log::Any::Formatter {
  has $.format = Nil;
  has $.template-format = Nil;
  has $!backend;
  has %.colors;
  has $.log-any-formatter;
  has $.colorize;

  use Log::Any::Formatter;
  submethod TWEAK {
    if $!template-format && $!format {
      die 'Only one of "template-format" or "format" should be specified.'
    }

    with $!template-format {
      when 'combined' {
        # Combined apache log format "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
        $!format = '\e{h} \e{l} \e{u} [\e{t}] "\e{r}" \e{s} \e{b} "\e{Referer}" \e{User-agent}';
      }
      when 'common' {
        # Common apache log format "%h %l %u %t \"%r\" %>s %b"
        $!format = '\e{h} \e{l} \e{u} [\e{t}] \e{r} \e{s} \e{b}';
      }
      when 'simple' {
        # "[%t] [%l] [pid %P] %F: %E: [client %a] %M"
        $!format = '[\e{t}] [\e{l}] [pid \e{P}] \e{F}: \e{E}: [client \e{a}] "\e{M}"';
      }
      when 'mpm' {
        #  [Fri Sep 09 10:42:29.902022 2011] [core:error] [pid 35708:tid 4328636416] [client 172.16.31.10] File does not exist: /usr/local/apache2/htdocs/favicon.ico
        # "[%{u}t] [%-m:%l] [pid %P:tid %T] %7F: %E: [client\ %a] %M% ,\ referer\ %{Referer}i"
        ...
      }
      default {
        die "Unknown value for 'template-format' ($!template-format)"
      }
    }

    with $!format {
      $!backend = Log::Any::FormatterBuiltIN.new(
        :format( $!format )
      );
    }

    $!backend //= Log::Any::FormatterBuiltIN.new(
      :format( '\m' ),
    );
  }

  method format( :$date-time, :$msg!, :$category!, :$severity!, :%extra-fields ) {
    my %data;
    my $query-uri = %extra-fields<REQUEST_URI>
      ?? %extra-fields<REQUEST_URI>.split('?')[0]
      !! '';
    my $request =
        ( %extra-fields<REQUEST_METHOD> || '' ) ~ ' '
      ~ ( %extra-fields<PATH_INFO> || $query-uri ) ~ ' '
      ~ ( %extra-fields<SERVER_PROTOCOL> || '' );

    sub dateTime-to-Str( DateTime:D $date-time ) {
      my @month = <Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec>;
{'fraction_non_alphanumeric': 0.22212065813528337, 'fraction_numerical': 0.02376599634369287, 'mean_word_length': 2.438334642576591, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5298479', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1712, 'n_words': 403}