| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Log::File | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Subclass of Badger::Log for logging messages to a file. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | #======================================================================== | ||
| 12 | |||
| 13 | package Badger::Log::File; | ||
| 14 | |||
| 15 | use Badger::Class | ||
| 16 | version => 0.01, | ||
| 17 | base => 'Badger::Log', | ||
| 18 | filesystem => 'FS', | ||
| 19 | config => [ | ||
| 20 | 'filesystem|method:FS', | ||
| 21 | 'filename|class:FILENAME', | ||
| 22 | 'keep_open|class:KEEP_OPEN=0', | ||
| 23 | ], | ||
| 24 | messages => { | ||
| 25 | no_filename => 'No filename specified for log file', | ||
| 26 | started => 'Started logging', | ||
| 27 | }; | ||
| 28 | |||
| 29 | |||
| 30 | sub init { | ||
| 31 | my ($self, $config) = @_; | ||
| 32 | $self->init_log($config); | ||
| 33 | $self->init_file($config); | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub init_file { | ||
| 38 | my ($self, $config) = @_; | ||
| 39 | |||
| 40 | # init_log() has already called configured() which will copy the filename | ||
| 41 | # parameter into $self. We don't make the filename parameter mandatory | ||
| 42 | # in the config schema because that will report missing parameters | ||
| 43 | # using error_msg() which has been redefined in Badger::Log. So we | ||
| 44 | # check for it here and use our "backdoor" _error_msg() method. | ||
| 45 | my $filename = $self->{ filename }; | ||
| 46 | |||
| 47 | return $self->_error_msg('no_filename') | ||
| 48 | unless defined $filename; | ||
| 49 | |||
| 50 | $self->{ file } = $self->{ filesystem }->file($filename); | ||
| 51 | |||
| 52 | $self->info_msg('started'); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub acquire { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | # Badger::Filesystem::File::append() method returns file handle open for append | ||
| 59 | return $self->{ handle } | ||
| 60 | ||= $self->{ file }->append; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub release { | ||
| 64 | my $self = shift; | ||
| 65 | my $fh = delete $self->{ handle } || return; | ||
| 66 | $fh->close() | ||
| 67 | # TODO: does IO::File throw an error on close fail? | ||
| 68 | # || return $self->base_error_msg( bad_close => $self->{ filename }, $! ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub log { | ||
| 72 | my ($self, $level, $message) = @_; | ||
| 73 | |||
| 74 | my $handle = $self->{ handle } | ||
| 75 | || $self->acquire; | ||
| 76 | |||
| 77 | my $output = sprintf($self->format($level, $message)); | ||
| 78 | $output =~ s/\n+$//; | ||
| 79 | $handle->printflush($output, "\n"); | ||
| 80 | |||
| 81 | $self->release unless $self->{ keep_open }; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | sub DESTROY { | ||
| 86 | shift->release; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ |