summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Logging.pm
blob: 769485c8640c636cbc6ec71a0d5f2f2d5cfcac62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Logging;
use 5.10.1;
use strict;
use warnings;

use Log::Log4perl;
use Log::Log4perl::MDC;
use File::Spec::Functions qw(rel2abs);
use Bugzilla::Constants qw(bz_locations);
use English qw(-no_match_vars $PROGRAM_NAME);

sub is_interactive {
    return not exists $ENV{SERVER_SOFTWARE}
}

BEGIN {
    my $file = $ENV{LOG4PERL_CONFIG_FILE} // 'log4perl-syslog.conf';
    Log::Log4perl::Logger::create_custom_level('NOTICE', 'WARN', 5, 2);
    Log::Log4perl->init(rel2abs($file, bz_locations->{confdir}));
    Log::Log4perl->get_logger(__PACKAGE__)->trace("logging enabled in $PROGRAM_NAME");
}

# this is copied from Log::Log4perl's :easy handling,
# except we also export NOTICE.
sub import {
    my $caller_pkg = caller;

    return 1 if $Log::Log4perl::IMPORT_CALLED{$caller_pkg}++;

    # Define default logger object in caller's package
    my $logger = Log::Log4perl->get_logger("$caller_pkg");

    # Define DEBUG, INFO, etc. routines in caller's package
    for (qw(TRACE DEBUG INFO NOTICE WARN ERROR FATAL ALWAYS)) {
        my $level = $_;
        $level = 'OFF' if $level eq 'ALWAYS';
        my $lclevel = lc $_;
        Log::Log4perl::easy_closure_create(
            $caller_pkg,
            $_,
            sub {
                Log::Log4perl::Logger::init_warn()
                  unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED;
                $logger->{$level}->( $logger, @_, $level );
            },
            $logger
        );
    }

    # Define LOGCROAK, LOGCLUCK, etc. routines in caller's package
    for (qw(LOGCROAK LOGCLUCK LOGCARP LOGCONFESS)) {
        my $method = 'Log::Log4perl::Logger::' . lc $_;

        Log::Log4perl::easy_closure_create(
            $caller_pkg,
            $_,
            sub {
                unshift @_, $logger;
                goto &$method;
            },
            $logger
        );
    }

    # Define LOGDIE, LOGWARN
    Log::Log4perl::easy_closure_create(
        $caller_pkg,
        'LOGDIE',
        sub {
            Log::Log4perl::Logger::init_warn()
              unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED;
            $logger->{FATAL}->( $logger, @_, 'FATAL' );
            $Log::Log4perl::LOGDIE_MESSAGE_ON_STDERR
              ? CORE::die( Log::Log4perl::Logger::callerline( join '', @_ ) )
              : exit $Log::Log4perl::LOGEXIT_CODE;
        },
        $logger
    );

    Log::Log4perl::easy_closure_create(
        $caller_pkg,
        'LOGEXIT',
        sub {
            Log::Log4perl::Logger::init_warn()
              unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED;
            $logger->{FATAL}->( $logger, @_, 'FATAL' );
            exit $Log::Log4perl::LOGEXIT_CODE;
        },
        $logger
    );

    Log::Log4perl::easy_closure_create(
        $caller_pkg,
        'LOGWARN',
        sub {
            Log::Log4perl::Logger::init_warn()
              unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED;
            $logger->{WARN}->( $logger, @_, 'WARN' );
            CORE::warn( Log::Log4perl::Logger::callerline( join '', @_ ) )
              if $Log::Log4perl::LOGDIE_MESSAGE_ON_STDERR;
        },
        $logger
    );
}

1;