summaryrefslogtreecommitdiffstats
path: root/lib/Log/Dispatch/Channels.pm
blob: c24a548769a38e05fcfaace239cec3ebe604db0c (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/perl
package Log::Dispatch::Channels;
use strict;
use warnings;
use Log::Dispatch;
use Carp;

# ABSTRACT: Adds separate logging channels to Log::Dispatch

=head1 SYNOPSIS

    use Log::Dispatch::Channels;

    my $logger = Log::Dispatch::Channels->new;
    $logger->add_channel('foo');
    my $timestamper = sub { my %p = @_; return time . $p{message}; };
    $logger->add_channel('bar', callbacks => $timestamper);
    $logger->add(Log::Dispatch::File->new(channels  => 'foo',
                                          name      => 'foo',
                                          min_level => 'debug',
                                          filename  => 'foo.log'));
    $logger->add(Log::Dispatch::Null->new(channels  => 'bar',
                                          name      => 'bar',
                                          min_level => 'debug'));
    $logger->add(Log::Dispatch::File->new(channels  => [qw/foo bar/],
                                          name      => 'errors',
                                          min_level => 'error',
                                          filename  => 'error.log'));
    $logger->log(channels => 'foo', level => 'debug',
                 message => 'For foo');
    $logger->log(channels => 'bar', level => 'error',
                 message => 'For bar and errors');

=head1 DESCRIPTION

This module manages a set of Log::Dispatch objects, treating them as separate
message channels to which messages can be logged. These objects can share
Log::Dispatch::Output objects, to allow for logging to multiple places
simultaneously and automatically.

=cut

=method new

=cut

sub new {
    my $class = shift;

    my $self = bless {
        channels => {},
        outputs  => {},
    }, $class;

    return $self;
}

=method add_channel

=cut

sub add_channel {
    my $self = shift;
    my $channel = shift;

    carp "Channel $channel already exists!"
        if exists $self->{channels}{$channel};

    $self->{channels}{$channel} = Log::Dispatch->new(@_);
}

=method remove_channel

=cut

sub remove_channel {
    my $self = shift;
    my $channel = shift;

    return delete $self->{channels}{$channel};
}

sub _forward_to_channels {
    my $self = shift;
    my $channels = shift;
    my $method = shift;
    my @channels = !defined $channels
                 ? (keys %{ $self->{channels} })
                 : ref $channels
                 ? @$channels
                 : ($channels);

    # XXX: sort of a hack - the return value is only used by would_log, which
    # just wants a boolean
    my $ret = 0;
    for my $channel (@channels) {
        if (exists $self->{channels}{$channel}) {
            my $methodret = $self->{channels}{$channel}->$method(@_);
            $ret ||= $methodret;
        }
        else {
            carp "Channel $channel doesn't exist";
        }
    }
    return $ret;
}

=method add

=cut

sub add {
    my $self = shift;
    my $output = shift;
    my %args = @_;

    carp "Output " . $output->name . " already exists!"
        if exists $self->{outputs}{$output->name};

    $self->_forward_to_channels($args{channels}, 'add', $output);
    $self->{outputs}{$output->name} = $output;
}

=method remove

=cut

sub remove {
    my $self = shift;
    my $output = shift;
    my %args = @_;

    $self->_forward_to_channels($args{channels}, 'remove', $output);
    return delete $self->{outputs}{$output};
}

=method log

=cut

sub log {
    my $self = shift;
    my %args = @_;
    my $channels = delete $args{channels};

    $self->_forward_to_channels($channels, 'log', %args);
}

=method log_and_die

=cut

sub log_and_die {
    my $self = shift;
    my %args = @_;
    my $channels = delete $args{channels};

    $self->_forward_to_channels($channels, 'log_and_die', %args);
}

=method log_and_croak

=cut

sub log_and_croak {
    my $self = shift;
    my %args = @_;
    my $channels = delete $args{channels};

    $self->_forward_to_channels($channels, 'log_and_croak', %args);
}

=method log_to

=cut

sub log_to {
    my $self = shift;
    my %args = @_;
    my $output = delete $args{name};

    $self->{outputs}{$output}->log(%args);
}

=method would_log

=cut

sub would_log {
    my $self = shift;
    my $level = shift;
    my %args = @_;
    my $channels = delete $args{channels};

    return $self->_forward_to_channels($channels, 'would_log', $level);
}

=method output

=cut

sub output {
    my $self = shift;
    my $output = shift;

    return $self->{outputs}{$output} if exists $self->{outputs}{$output};
    return undef;
}

=method channel

=cut

sub channel {
    my $self = shift;
    my $channel = shift;

    return $self->{channels}{$channel} if exists $self->{channels}{$channel};
    return undef;
}

1;