Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a disk based store. #1

Merged
merged 3 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ sudo: false
perl:
- "5.16"
- "5.14"
- "5.12"
- "5.10"

install:
- cpanm --notest --quiet Dist::Zilla
- cpanm --notest --quiet Dist::Zilla@5.047
- dzil authordeps | cpanm --quiet --notest
- cpanm --quiet --notest --installdeps .

Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ requires 'DateTime';
requires 'Log::Log4perl', '>=1.42';
requires 'Moose', '>=2.1005';

test_requires 'File::Path';
test_requires 'File::Temp';
test_requires 'File::Slurp';
test_requires 'Test::More';
Expand Down
85 changes: 85 additions & 0 deletions lib/Log/Log4perl/Appender/Chunk/Store/File.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package Log::Log4perl::Appender::Chunk::Store::File;

use Moose;
extends qw/Log::Log4perl::Appender::Chunk::Store/;

use Log::Log4perl;
use File::Path ();
use File::Spec ();
use File::Temp ();

my $LOGGER = Log::Log4perl->get_logger();

has 'base_directory' => ( is => 'ro' , isa => 'Str' , default => sub { File::Temp::tempdir() });
has 'log_folder' => ( is => 'ro' , isa => 'Str' , default => sub{ {'Log4perl_Appender_Chunk_Store_File'}; });

has '_logging_folder' => ( is => 'ro' , lazy_build => 1 );

sub _build__logging_folder {
my ($self)= @_;
return File::Spec->catfile($self->base_directory, $self->log_folder)
}

sub store{
my ($self, $chunk_id, $big_message) = @_;
my $logging_dir = $self->_logging_folder();
unless ( -d $logging_dir ) {
eval { File::Path::mkpath($logging_dir) };
if ($@) {
$LOGGER->trace("Couldn't create $logging_dir: $@");
return;
}
}
$LOGGER->trace("Storing chunk ".$chunk_id);
open my $FH, '>', File::Spec->catfile($logging_dir, $chunk_id);
print $FH $big_message;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use File::Slurp (with utf8)

close $FH;
return 1;
Copy link
Owner

@jeteve jeteve Dec 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log 'stored chunk ID chunk ID in this place

}

__PACKAGE__->meta->make_immutable();
1;
__END__

=head1 NAME

Log::Log4perl::Appender::Chunk::Store::File - Stores chunks on disk

=head1 SYNOPSIS

Fist make sure you read L<Log::Log4perl::Appender::Chunk> documentation.

l4p.conf:

log4perl.rootLogger=TRACE, Chunk

layout_class=Log::Log4perl::Layout::PatternLayout
layout_pattern=%m%n

log4perl.appender.Chunk=Log::Log4perl::Appender::Chunk

# Built-in store class File
log4perl.appender.Chunk.store_class=File
log4perl.appender.Chunk.store_args.base_directory=/tmp/
log4perl.appender.Chunk.store_args.log_folder=chucks


See L<Log::Log4perl::Appender::Chunk>'s synopsis for a more complete example.

=head1 OPTIONS

=over

=item base_directory

Optional. This is the root folder for where the chunks live on the disk.

=item log_folder

Optional. The sub folder inside of the base directory where the chunks live.

=head2 store

See L<Log::Log4perl::Appender::Chunk::Store>

=cut
47 changes: 47 additions & 0 deletions t/06-store-file.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#! perl -T
use strict;
use Test::More;

use Log::Log4perl;
use File::Temp ();
use File::Path ();
use File::Slurp ();

my $base_directory = File::Temp::tempdir();
my $log_folder = 'test_folder';

my $conf = q|
log4perl.rootLogger=TRACE, Chunk

layout_class=Log::Log4perl::Layout::PatternLayout
layout_pattern=%m%n

log4perl.appender.Chunk=Log::Log4perl::Appender::Chunk
log4perl.appender.Chunk.store_class=File
log4perl.appender.Chunk.store_args.base_directory=|. $base_directory .q|
log4perl.appender.Chunk.store_args.log_folder=|. $log_folder .q|

log4perl.appender.Chunk.layout=${layout_class}
log4perl.appender.Chunk.layout.ConversionPattern=${layout_pattern}
|;

Log::Log4perl::init(\$conf);

ok( my $ca = Log::Log4perl->appender_by_name('Chunk') , "Ok got Chunk appender");
ok( my $store = $ca->store(), "Ok got store for the logger");
is( $store->base_directory(), $base_directory, "Ok got a base_directory");
is( $store->log_folder(), $log_folder , "default log folder");

ok( $store->store('a_key' , 'Some big content'), "Ok can store stuff");

my $chunk_file = File::Spec->catfile($store->_logging_folder, 'a_key' );
my $file_content = File::Slurp::read_file( $chunk_file );
like($file_content, qr/Some big content/, 'the chunck file contains the right thing');

# some cleanup
END {
File::Path::rmtree($base_directory)
}


done_testing();