Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1811 from metacpan/haarg/configurable-log4perl
configurable log4perl
  • Loading branch information
oalders committed Nov 22, 2016
2 parents d622717 + ecfadbd commit 8790674
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,6 +14,7 @@
carton.lock
cover_db/
local
log4perl_local.conf
metacpan_web_local.conf
perltidy.LOG
root/static/sitemaps
Expand Down
40 changes: 24 additions & 16 deletions app.psgi
@@ -1,4 +1,4 @@
package MetaCPAN::Web; ## no critic (RequireFilenameMatchesPackage)
package MetaCPAN::Web::App; ## no critic (RequireFilenameMatchesPackage)

# ABSTRACT: Modern front-end for MetaCPAN

Expand All @@ -8,32 +8,45 @@ use warnings;
# TODO: When we know everything will work reliably: $ENV{PLACK_ENV} ||= 'development';
#
use File::Basename;
use Config::JFDI;
use Log::Log4perl;
use File::Spec;
use File::Path ();
use Plack::Builder;

my $root_dir;
my $dev_mode;
my $config;

BEGIN {
$root_dir = File::Basename::dirname(__FILE__);
$dev_mode = $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development';
}
$config = Config::JFDI->new(
name => 'MetaCPAN::Web',
path => $root_dir,
);

BEGIN {
if ($dev_mode) {
$ENV{PLACK_SERVER} = 'Standalone';
$ENV{METACPAN_WEB_DEBUG} = 1;
}
}

use lib "$root_dir/lib";
use Config::JFDI;
use File::Path ();
use MetaCPAN::Web;
use Plack::Builder;
my $log4perl_config = File::Spec->rel2abs( $config->get->{log4perl_file}
|| 'log4perl.conf', $root_dir );
Log::Log4perl::init($log4perl_config);

# use a unique package and tell l4p to ignore it when finding the warning location.
package MetaCPAN::Web::WarnHandler;
Log::Log4perl->wrapper_register(__PACKAGE__);
my $logger = Log::Log4perl->get_logger;
$SIG{__WARN__} = sub { $logger->warn(@_) };

BEGIN {
$SIG{__WARN__} = sub { MetaCPAN::Web->log->warn(@_) };
$dev_mode and require Devel::Confess and Devel::Confess->import;
}

use lib "$root_dir/lib";
use MetaCPAN::Web;

my $tempdir = "$root_dir/var/tmp";

# explicitly call ->to_app on every Plack::App::* for performance
Expand All @@ -60,11 +73,6 @@ builder {
);

builder {
my $config = Config::JFDI->new(
name => 'MetaCPAN::Web',
path => $root_dir,
);

die 'cookie_secret not configured'
unless $config->get->{cookie_secret};

Expand Down
15 changes: 1 addition & 14 deletions lib/MetaCPAN/Web.pm
Expand Up @@ -47,20 +47,7 @@ sub token {
shift->request->session->get('token');
}

__PACKAGE__->log(
Log::Log4perl::Catalyst->new(
\q{
log4perl.rootLogger=DEBUG, OUTPUT
log4perl.appender.OUTPUT=Log::Log4perl::Appender::Screen
log4perl.appender.OUTPUT.stderr=1
log4perl.appender.OUTPUT.layout=PatternLayout
log4perl.appender.OUTPUT.layout.ConversionPattern=[%d] [%p] [%X{url}] %m%n
},
autoflush => 1,
)
);
__PACKAGE__->log( Log::Log4perl::Catalyst->new( undef, autoflush => 1 ) );

__PACKAGE__->setup();
__PACKAGE__->meta->make_immutable;
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Web/Controller/Root.pm
@@ -1,5 +1,6 @@
package MetaCPAN::Web::Controller::Root;
use Moose;
use Log::Log4perl::MDC;
use namespace::autoclean;

BEGIN { extends 'MetaCPAN::Web::Controller' }
Expand Down
8 changes: 7 additions & 1 deletion lib/MetaCPAN/Web/Model/API.pm
Expand Up @@ -13,6 +13,7 @@ use MetaCPAN::Web::Types qw( Uri );
use MooseX::ClassAttribute;
use Try::Tiny qw( catch try );
use URI::QueryParam;
use Log::Log4perl;

class_has client => (
is => 'ro',
Expand Down Expand Up @@ -74,14 +75,19 @@ sub request {
$url->query_param( $param => $params->{$param} );
}

my $current_url = Log::Log4perl::MDC->get('url');

my $request = HTTP::Request->new(
(
$method ? $method
: $search ? 'POST'
: 'GET',
),
$url,
( $search ? [ 'Content-type' => 'application/json' ] : () ),
[
( $search ? ( 'Content-Type' => 'application/json' ) : () ),
( $current_url ? ( 'Referer' => $current_url ) : () ),
],
);

# encode_json returns an octet string
Expand Down
7 changes: 7 additions & 0 deletions log4perl.conf
@@ -0,0 +1,7 @@
log4perl.rootLogger=DEBUG, OUTPUT

log4perl.appender.OUTPUT=Log::Log4perl::Appender::Screen
log4perl.appender.OUTPUT.stderr=1

log4perl.appender.OUTPUT.layout=PatternLayout
log4perl.appender.OUTPUT.layout.ConversionPattern=[%d] [%p] [%X{url}] %m%n

0 comments on commit 8790674

Please sign in to comment.