Skip to content

Commit

Permalink
Merge branch 'topic/engines'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Sukrieh committed Dec 10, 2011
2 parents 0c75eb6 + b2d3abc commit 7761205
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Makefile.old
MYMETA.json
MYMETA.yml
contrib/sessions/
contrib/logs/
5 changes: 4 additions & 1 deletion contrib/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ appname: test
traces: 1
charset: 'UTF8'
content_type: "text/plain"
logger: "console"
logger: "File"
template: "template_toolkit"

session: 'YAML'
Expand All @@ -14,6 +14,9 @@ engines:
serializer:
json:
pretty: 1
logger:
File:
log_dir: "/tmp/testlogs"

plugins:
Foo:
Expand Down
1 change: 1 addition & 0 deletions contrib/test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Dancer::Plugin::Bar;

get '/' => sub {
debug "in / route";
to_yaml(session());
};

Expand Down
1 change: 1 addition & 0 deletions lib/Dancer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ sub import {
# the app object
my $app = Dancer::Core::App->new(
name => $caller,
environment => $runner->environment,
location => $runner->location,
runner_config => $runner->config,
);
Expand Down
14 changes: 10 additions & 4 deletions lib/Dancer/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ has location => (
default => sub { File::Spec->rel2abs('.') },
);

sub config_location { goto &location }

has environment => (
is => 'ro',
isa => sub { Str(@_) },
default => sub { 'development' },
);

sub get_environment { goto &environment }

has runner_config => (
is => 'ro',
isa => sub { HashRef(@_) },
Expand Down Expand Up @@ -106,10 +116,6 @@ sub template {
return $content;
}

# we dont support per-app config files yet
# (but that could be easy to do in the future)
sub config_location { undef }
sub get_environment { undef }

sub supported_hooks {
qw/before after before_request after_request/
Expand Down
28 changes: 15 additions & 13 deletions lib/Dancer/Core/Role/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,12 @@ sub _normalize_config_entry {

my $_setters = {
logger => sub {
my ($self, $value) = @_;

return (ref $value)
? $value
: Dancer::Factory::Engine->create(logger => $value);
my ($self, $value, $config) = @_;
return $value if ref($value);
my $engine_options = $self->_get_config_for_engine(logger => $value, $config);
return Dancer::Factory::Engine->create(logger => $value, %{$engine_options});
},

# log_file => sub {
# Dancer::Logger->init(setting("logger"), setting());
# },
session => sub {
my ($self, $value, $config) = @_;
return $value if ref($value);
Expand All @@ -170,6 +166,7 @@ my $_setters = {
$engine_options->{session_dir} ||= File::Spec->catdir($self->config_location, 'sessions');
return Dancer::Factory::Engine->create(session => $value, %{$engine_options});
},

template => sub {
my ($self, $value, $config) = @_;
return $value if ref($value);
Expand Down Expand Up @@ -220,15 +217,20 @@ sub _compile_config_entry {
sub _get_config_for_engine {
my ($self, $engine, $name, $config) = @_;

return {} unless defined $config->{engines};
my $default_config = {
environment => $self->get_environment,
location => $self->config_location,
};
return $default_config unless defined $config->{engines};

if (! defined $config->{engines}{$engine}) {
carp "No config section for engines/$engine "
. "(unable to find configuration for $name)";
return {};
return $default_config;
}

return $config->{engines}{$engine}{$name} || {};
return {
%{ $default_config },
%{ $config->{engines}{$engine}{$name} } ,
} || $default_config;
}

1;
3 changes: 3 additions & 0 deletions lib/Dancer/Core/Role/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ with 'Dancer::Core::Role::Hookable';

requires 'type';

has environment => (is => 'ro');
has location => (is => 'ro');

has context => (
is => 'rw',
isa => sub { ObjectOf('Dancer::Core::Context', @_) },
Expand Down
119 changes: 119 additions & 0 deletions lib/Dancer/Logger/File.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package Dancer::Logger::File;
use Carp 'carp';
use Moo;
use Dancer::Moo::Types;

with 'Dancer::Core::Role::Logger';

use File::Spec;
use Dancer::FileUtils qw(open_file);
use IO::File;

has log_dir => (
is => 'rw',
isa => sub { Str(@_) },
trigger => sub {
my ($self, $dir) = @_;
if (! -d $dir && ! mkdir $dir) {
return carp "Log directory \"$dir\" does not exist and unable to create it.";
}
return carp "Log directory \"$dir\" is not writable." if ! -w $dir;
},
builder => '_build_log_dir',
lazy => 1,
);

sub _build_log_dir {
my ($self) = @_;
return $self->config->{logdir} ||
File::Spec->catdir($self->location, 'logs');
}

has file_name => (
is => 'ro',
isa => sub { Str(@_) },
builder => '_build_file_name',
lazy => 1
);

sub _build_file_name {
my ($self) = @_;
my $env = $self->environment;
return "$env.log";
}

has log_file => ( is => 'rw', isa => sub { Str(@_) } );
has fh => ( is => 'rw' );

sub BUILD {
my $self = shift;
my $logfile = File::Spec->catfile($self->log_dir, $self->file_name);

my $fh;
unless($fh = open_file('>>', $logfile)) {
carp "unable to create or append to $logfile";
return;
}
$fh->autoflush;
$self->log_file($logfile);
$self->fh($fh);
}

sub _log {
my ($self, $level, $message) = @_;
my $fh = $self->fh;

return unless(ref $fh && $fh->opened);

$fh->print($self->format_message($level => $message))
or carp "writing to logfile $self->{logfile} failed";
}

1;

__END__
=head1 NAME
Dancer::Logger::File - file-based logging engine for Dancer
=head1 SYNOPSIS
=head1 DESCRIPTION
This is a file-based logging engine that allows you to save your logs to files
on disk.
=head1 METHODS
=head2 init
This method is called when C<< ->new() >> is called. It initializes the log
directory, creates if it doesn't already exist and opens the designated log
file.
=head2 logdir
Returns the log directory, decided by "logs" either in "appdir" setting.
It's also possible to specify a logs directory with the log_path option.
setting log_path => $dir;
=head2 _log
Writes the log message to the file.
=head1 AUTHOR
Alexis Sukrieh
=head1 LICENSE AND COPYRIGHT
Copyright 2009-2010 Alexis Sukrieh.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
2 changes: 1 addition & 1 deletion t/app.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ for my $p ('/', '/mywebsite') {
}
}

is $app->get_environment, undef;
is $app->get_environment, 'development';

my $routes_regexps = $app->routes_regexps_for('get');
is (scalar(@$routes_regexps), 4, "route regexps are OK");
Expand Down

0 comments on commit 7761205

Please sign in to comment.