Skip to content

Commit

Permalink
Merge pull request #1017 from kraih/log_attribute
Browse files Browse the repository at this point in the history
move default log behavior to overloaded attribute
  • Loading branch information
kraih committed Nov 28, 2016
2 parents c327a65 + b5f87cd commit c895cab
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,7 +1,9 @@

7.11 2016-11-25
7.11 2016-11-28
- Improved one_tick method in Mojo::IOLoop to protect from recursion, similar
to the start method.
- Improved Mojolicious log attribute making it easier to override default
settings. (jberger)

7.10 2016-11-01
- Added getopt function to Mojo::Util.
Expand Down
50 changes: 34 additions & 16 deletions lib/Mojolicious.pm
Expand Up @@ -23,6 +23,23 @@ has commands => sub {
return $commands;
};
has controller_class => 'Mojolicious::Controller';

has log => sub {
my $self = shift;
my $log = Mojo::Log->new;
my $home = $self->home;
my $mode = $self->mode;

# Check if we have a log directory that is writable
$log->path($home->rel_file("log/$mode.log"))
if -d $home->rel_file('log') && -w _;

# Reduced log output outside of development mode
$log->level('info') unless $mode eq 'development';

return $log;
};

has mode => sub { $ENV{MOJO_MODE} || $ENV{PLACK_ENV} || 'development' };
has moniker => sub { Mojo::Util::decamelize ref shift };
has plugins => sub { Mojolicious::Plugins->new };
Expand Down Expand Up @@ -151,20 +168,12 @@ sub new {
$r->hide(qw(rendered req res respond_to send session signed_cookie stash));
$r->hide(qw(tx url_for validation write write_chunk));

# Check if we have a log directory that is writable
my $mode = $self->mode;
$self->log->path($home->rel_file("log/$mode.log"))
if -d $home->rel_file('log') && -w _;

$self->plugin($_)
for qw(HeaderCondition DefaultHelpers TagHelpers EPLRenderer EPRenderer);

# Exception handling should be first in chain
$self->hook(around_dispatch => \&_exception);

# Reduced log output outside of development mode
$self->log->level('info') unless $mode eq 'development';

$self->startup;

return $self;
Expand Down Expand Up @@ -393,16 +402,26 @@ Class to be used for the default controller, defaults to
L<Mojolicious::Controller>. Note that this class needs to have already been
loaded before the first request arrives.
=head2 log
my $log = $app->log;
$app = $app->log(Mojo::Log->new);
The logging layer of your application, defaults to a L<Mojo::Log> object. The
level will default to C<debug> if the L</mode> is C<development> or C<info>
otherwise. All messages will be written to C<STDERR> or a C<log/$mode.log> file
if a C<log> directory exists.
# Log debug message
$app->log->debug('It works');
=head2 mode
my $mode = $app->mode;
$app = $app->mode('production');
The operating mode for your application, defaults to a value from the
C<MOJO_MODE> and C<PLACK_ENV> environment variables or C<development>. Right
before calling L</"startup">, L<Mojolicious> will pick up the current mode,
name the log file after it and raise the log level from C<debug> to C<info> if
it has a value other than C<development>.
C<MOJO_MODE> and C<PLACK_ENV> environment variables or C<development>.
=head2 moniker
Expand Down Expand Up @@ -634,10 +653,9 @@ requests indiscriminately, for a full list of available hooks see L</"HOOKS">.
my $app = Mojolicious->new({moniker => 'foo_bar'});
Construct a new L<Mojolicious> application and call L</"startup">. Will
automatically detect your home directory and set up logging based on your
current operating mode. Also sets up the renderer, static file server, a
default set of plugins and an L</"around_dispatch"> hook with the default
exception handling.
automatically detect your home directory. Also sets up the renderer, static file
server, a default set of plugins and an L</"around_dispatch"> hook with the
default exception handling.
=head2 plugin
Expand Down
7 changes: 6 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -449,10 +449,15 @@ $t->get_ok('/just/some/template')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is("Development template with high precedence.\n");

# Check develpment mode log level
# Check default development mode log level
my $app = Mojolicious->new;
is $app->log->level, 'debug', 'right log level';

# Check non-development mode log level
$app = Mojolicious->new;
$app->mode('test');
is $app->log->level, 'info', 'right log level';

# Make sure we can override attributes with constructor arguments
$app = MojoliciousTest->new(mode => 'test');
is $app->mode, 'test', 'right mode';
Expand Down

0 comments on commit c895cab

Please sign in to comment.