Permalink
Browse files

Merge branch 'topic/engines'

  • Loading branch information...
2 parents 0c75eb6 + b2d3abc commit 776120534e91b88f2acbce34d526b0cceb48efdd Alexis Sukrieh committed Dec 10, 2011
Showing with 155 additions and 19 deletions.
  1. +1 −0 .gitignore
  2. +4 −1 contrib/config.yml
  3. +1 −0 contrib/test.pl
  4. +1 −0 lib/Dancer.pm
  5. +10 −4 lib/Dancer/Core/App.pm
  6. +15 −13 lib/Dancer/Core/Role/Config.pm
  7. +3 −0 lib/Dancer/Core/Role/Engine.pm
  8. +119 −0 lib/Dancer/Logger/File.pm
  9. +1 −1 t/app.t
View
1 .gitignore
@@ -8,3 +8,4 @@ Makefile.old
MYMETA.json
MYMETA.yml
contrib/sessions/
+contrib/logs/
View
5 contrib/config.yml
@@ -2,7 +2,7 @@ appname: test
traces: 1
charset: 'UTF8'
content_type: "text/plain"
-logger: "console"
+logger: "File"
template: "template_toolkit"
session: 'YAML'
@@ -14,6 +14,9 @@ engines:
serializer:
json:
pretty: 1
+ logger:
+ File:
+ log_dir: "/tmp/testlogs"
plugins:
Foo:
View
1 contrib/test.pl
@@ -4,6 +4,7 @@
use Dancer::Plugin::Bar;
get '/' => sub {
+ debug "in / route";
to_yaml(session());
};
View
1 lib/Dancer.pm
@@ -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,
);
View
14 lib/Dancer/Core/App.pm
@@ -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(@_) },
@@ -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/
View
28 lib/Dancer/Core/Role/Config.pm
@@ -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);
@@ -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);
@@ -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;
View
3 lib/Dancer/Core/Role/Engine.pm
@@ -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', @_) },
View
119 lib/Dancer/Logger/File.pm
@@ -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.
+
View
2 t/app.t
@@ -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");

0 comments on commit 7761205

Please sign in to comment.