-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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; | ||
close $FH; | ||
return 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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,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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)