Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
163 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use v6; | ||
|
||
unit class Apache::LogFormat; | ||
use Apache::LogFormat::Compiler; | ||
|
||
method common(Apache::LogFormat:U: :$logger) { | ||
my $p = Apache::LogFormat::Compiler.new(); | ||
return $p.compile('%h %l %u %t "%r" %>s %b'); | ||
} | ||
|
||
method combined(Apache::LogFormat:U: :$logger) { | ||
my $p = Apache::LogFormat::Compiler.new(); | ||
return $p.compile('%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"'); | ||
} | ||
|
||
=begin pod | ||
=head1 NAME | ||
Apache::LogFormat - Provide Apache-Style Log Generators | ||
=head1 SYNOPSIS | ||
# Use a predefined log format to generate string for logging | ||
use Apache::LogFormat; | ||
my $fmt = Apache::LogFormat.combined; | ||
my $line = $fmt.format(%env, @res, $length, $reqtime, $time); | ||
$*ERR.print($line); | ||
# Compile your own log formatter | ||
use Apache::LogFormat::Compiler; | ||
my $c = Apache::LogFormat::Compiler.new; | ||
my $fmt = $c.compile(' ... pattern ... '); | ||
my $line = $fmt.format(%env, @res, $length, $reqtime, $time); | ||
$*ERR.print($line); | ||
=head1 DESCRIPTION | ||
Apache::LogFormat provides Apache-style log generators. | ||
=head1 PRE DEFINED FORMATTERS | ||
=head2 common(): $fmt:Apache::LogFormat::Formatter | ||
Creates a new Apache::LogFormat::Formatter that generates log lines in | ||
the following format: | ||
%h %l %u %t "%r" %>s %b | ||
=head2 combined(): $fmt:Apache::LogFormat::Formatter | ||
Creates a new Apache::LogFormat::Formatter that generates log lines in | ||
the following format: | ||
%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i" | ||
=head1 AUTHOR | ||
Daisuke Maki <lestrrat@gmail.com> | ||
=head1 COPYRIGHT AND LICENSE | ||
Copyright 2015 Daisuke Maki | ||
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0. | ||
=end pod | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use v6; | ||
|
||
unit class Apache::LogFormat::Formatter; | ||
|
||
has &!callback; | ||
|
||
method new(&callback) { | ||
return self.bless(:&callback); | ||
} | ||
|
||
submethod BUILD(:&!callback) { } | ||
|
||
# %env is the PSGI environment hash | ||
# @res is a 3 item list, in PSGI style | ||
method format(Apache::LogFormat::Formatter:D: %env, @res) { | ||
# TODO: provide proper parameters to callback | ||
my $time = DateTime.now(); | ||
return &!callback(%env, @res, Nil, Nil, $time) ~ "\n"; | ||
} | ||
|
||
|
||
=begin pod | ||
=head1 NAME | ||
Apache::LogFormat::Formatter - Creates Log Lines | ||
=head1 SYNOPSIS | ||
use Apache::LogFormat::Formatter; | ||
my $fmt = Apache::LogFormat::Formatter.new(sub(%env, @res, $length, $reqtime, $time) { | ||
... | ||
}); | ||
my $line = $fmt.format(%env, @res, $length, $reqtime, $time); | ||
$*ERR.print($line); | ||
=head1 DESCRIPTION | ||
Apache::LogFormat::Formatter creates strings out of bunch of data. You usually | ||
do not have to create one yourself, as it will be created by Apache::LogFormat::Compiler. | ||
=head1 AUTHOR | ||
Daisuke Maki <lestrrat@gmail.com> | ||
=head1 COPYRIGHT AND LICENSE | ||
Copyright 2015 Daisuke Maki | ||
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0. | ||
=end pod | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters