Skip to content

Commit

Permalink
Re-package
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Oct 14, 2015
1 parent 6d70363 commit 7d3efca
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 55 deletions.
12 changes: 8 additions & 4 deletions META6.json
@@ -1,13 +1,17 @@
{
"name" : "Apache::LogFormat::Compiler",
"name" : "Apache::LogFormat",
"source-url" : "git://github.com/lestrrat/p6-Apache-LogFormat-Compiler.git",
"perl" : "v6",
"build-depends" : [ ],
"provides" : {
"Apache::LogFormat::Compiler" : "lib/Apache/LogFormat/Compiler.pm6"
"Apache::LogFormat::Compiler" : "lib/Apache/LogFormat/Compiler.pm6",
"Apache::LogFormat" : "lib/Apache/LogFormat.pm6",
"Apache::LogFormat::Formatter" : "lib/Apache/LogFormat/Formatter.pm6"
},
"depends" : [ ],
"description" : "blah blah blah",
"depends" : [
"DateTime::Format"
],
"description" : "Provide Apache-Style Log Generators",
"test-depends" : [ ],
"version" : "*",
"authors" : [
Expand Down
21 changes: 19 additions & 2 deletions README.md
Expand Up @@ -3,7 +3,7 @@
NAME
====

Apache::LogFormat::Compiler - blah blah blah
Apache::LogFormat - Provide Apache-Style Log Generators

SYNOPSIS
========
Expand All @@ -24,7 +24,24 @@ SYNOPSIS
DESCRIPTION
===========

Apache::LogFormat::Compiler is ...
Apache::LogFormat provides Apache-style log generators.

PRE DEFINED FORMATTERS
======================

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

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"

AUTHOR
======
Expand Down
68 changes: 68 additions & 0 deletions lib/Apache/LogFormat.pm6
@@ -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

62 changes: 14 additions & 48 deletions lib/Apache/LogFormat/Compiler.pm6
@@ -1,26 +1,7 @@
use v6;

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";
}

}

class Apache::LogFormat::Compiler {
unit class Apache::LogFormat::Compiler;
use Apache::LogFormat::Formatter;

use DateTime::Format;

Expand Down Expand Up @@ -158,40 +139,14 @@ method compile(Apache::LogFormat::Compiler:D: $pat, %extra-blocks?, %extra-chars
return Apache::LogFormat::Formatter.new($code);
}
}
class Apache::LogFormat {
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::Compiler - blah blah blah
Apache::LogFormat::Compiler - Compiles Log Format Into Apache::LogFormat::Formatter
=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 ... ');
Expand All @@ -203,6 +158,17 @@ Apache::LogFormat::Compiler - blah blah blah
Apache::LogFormat::Compiler compiles an Apache-style log format string into
efficient perl6 code. It was originally written for perl5 by kazeburo.
=head1 METHODS
=head2 new(): $compiler:Apache::LogFormat::Compiler
Creates a new parser. The parser is stateless, so you can reuse it as many
times to compile log patterns.
=head2 compile($pat:String, %extra-block-handlers:Hash(Str,Callable), %extra-char-handlers:Hash(Str,Callable)) $fmt:Apache::LogFormat::Formatter
Compiles the pattern into an executable formatter object.
=head1 AUTHOR
Daisuke Maki <lestrrat@gmail.com>
Expand Down
53 changes: 53 additions & 0 deletions lib/Apache/LogFormat/Formatter.pm6
@@ -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

2 changes: 1 addition & 1 deletion t/02-predefined.t
@@ -1,6 +1,6 @@
use v6;
use Test;
use Apache::LogFormat::Compiler;
use Apache::LogFormat;

my $fmt = Apache::LogFormat.combined();
if ! isa-ok($fmt, "Apache::LogFormat::Formatter") {
Expand Down

0 comments on commit 7d3efca

Please sign in to comment.