summaryrefslogtreecommitdiffstats
path: root/lib/Jaos/WebApp/Log.pm
blob: 167d91758af247f1f941d725c7784f3adfb8689d (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
114
115
116
117
package Jaos::WebApp::Log;
use IO::File;
use strict;
use warnings;
use File::Basename ();
use File::Path ();

sub new
{
  my $class   = shift;
  my $self    = {@_};

  if ($self->{logfile} ) {
    my $logdir = File::Basename::dirname($self->{logfile});
    File::Path::mkpath($logdir);
    $self->{fh} = IO::File->new($self->{logfile}, 'a') or die "Failed to open logfile: $@";
  } else {
    $self->{fh} = \*STDERR;
  }

  $self->{loglevel} ||= 'error';
  $self->{level_weight} = { debug => 0, info => 1, error => 2};

  bless $self, $class;
}

sub debug    { shift->log('debug', @_); };
sub info     { shift->log('info', @_); };
sub error    { shift->log('error', @_); };

sub level    { shift->{loglevel} = shift; }
sub is_level {
  my ($self, $level) = @_;
  my $weight         = $self->{level_weight}->{$level};
  my $current_weight = $self->{level_weight}->{$self->{loglevel}};
  $current_weight <= $weight;
}

sub log
{
  my ($self, $level, @msgs) = @_;

  return unless $self->is_level($level);

  my $time = localtime(time);
  my $msgs = join("\n", @msgs);
  my ($pkg, $line) = (caller())[0, 2];
  ($pkg, $line) = (caller(1))[0, 2] if $pkg eq ref $self;


  $self->{fh}->syswrite("[$$] $time $level $pkg:$line $msgs\n");
}

=head1 NAME

Jaos::WebApp::Log - Jaos::WebApp logging

=head1 SYNOPSIS

 my $d = Jaos::WebApp::Log->new( logfile => '/path/to/log', loglevel => 'debug');
 $d->debug("this is life");


 # or via Jaos::WebApp creation
 my $app = MyApp->new( logfile => '/path/to/log', loglevel => 'info');

=head1 DESCRIPTION

This is the logging mechanism for L<Jaos::WebApp> that logs to STDERR or the B<logfile> specified.

=head1 METHODS

=head2 new

Constructor.

=head2 debug

Log a debugging message.

=head2 info

Log an informational message.

=head2 error

Log an error message.

=head2 level

Set the default logging level.  Anything less less than this level is not logged.

=head1 AUTHOR

Jason Woodward <woodwardj@jaos.org>

=head1 LICENSE

Copyright (C) 2010-2011 Jason Woodward
All rights reserved

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library.  If not, see <http://www.gnu.org/licenses/>.

=cut

1;