| 1 | package XAS::Lib::Modules::Log::Syslog; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use Params::Validate 'HASHREF'; | ||
| 6 | use Sys::Syslog qw(:standard :extended); | ||
| 7 | |||
| 8 | use XAS::Class | ||
| 9 | version => $VERSION, | ||
| 10 | base => 'XAS::Base', | ||
| 11 | mixins => 'init_log output destroy', | ||
| 12 | ; | ||
| 13 | |||
| 14 | # ---------------------------------------------------------------------- | ||
| 15 | # Public Methods | ||
| 16 | # ---------------------------------------------------------------------- | ||
| 17 | |||
| 18 | sub output { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | $self = $self->prototype() unless ref $self; | ||
| 22 | |||
| 23 | my ($args) = $self->validate_params(\@_, [ | ||
| 24 | { type => HASHREF } | ||
| 25 | ]); | ||
| 26 | |||
| 27 | my $priority = _translate($args->{priority}); | ||
| 28 | my $message = sprintf('%s', $args->{message}); | ||
| 29 | |||
| 30 | syslog($priority, $message); | ||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | sub destroy { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | closelog(); | ||
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | # ---------------------------------------------------------------------- | ||
| 42 | # Private Methods | ||
| 43 | # ---------------------------------------------------------------------- | ||
| 44 | |||
| 45 | sub _translate { | ||
| 46 | my $value = shift; | ||
| 47 | |||
| 48 | my $translate = { | ||
| 49 | info => 'info', | ||
| 50 | error => 'err', | ||
| 51 | warn => 'warning', | ||
| 52 | fatal => 'alert', | ||
| 53 | trace => 'notice', | ||
| 54 | debug => 'debug' | ||
| 55 | }; | ||
| 56 | |||
| 57 | return $translate->{lc($value)}; | ||
| 58 | |||
| 59 | } | ||
| 60 | |||
| 61 | sub init_log { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | setlogsock('unix'); | ||
| 65 | openlog($self->process, 'pid', $self->facility); | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ | ||
| 72 | |||
| 73 | =head1 NAME | ||
| 74 | |||
| 75 | XAS::Lib::Modules::Log::Syslog - A mixin class for logging | ||
| 76 | |||
| 77 | =head1 DESCRIPTION | ||
| 78 | |||
| 79 | This module is a mixin for logging. This logs to syslog. | ||
| 80 | |||
| 81 | =head1 METHODS | ||
| 82 | |||
| 83 | =head2 init_log | ||
| 84 | |||
| 85 | This method initializes syslog. Sets the process, facility and requests that | ||
| 86 | the pid be included. | ||
| 87 | |||
| 88 | =head2 output($hashref) | ||
| 89 | |||
| 90 | This method translate the log level to an appropriate syslog priority and | ||
| 91 | writes out the log line. The translation is a follows: | ||
| 92 | |||
| 93 | info => 'info', | ||
| 94 | error => 'err', | ||
| 95 | warn => 'warning', | ||
| 96 | fatal => 'alert', | ||
| 97 | trace => 'notice', | ||
| 98 | debug => 'debug' | ||
| 99 | |||
| 100 | =head2 destroy | ||
| 101 | |||
| 102 | Closes the connection to syslog. | ||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | =over 4 | ||
| 107 | |||
| 108 | =item L<XAS|XAS> | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | =head1 AUTHOR | ||
| 113 | |||
| 114 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 115 | |||
| 116 | =head1 COPYRIGHT AND LICENSE | ||
| 117 | |||
| 118 | Copyright (C) 2014 Kevin L. Esteb | ||
| 119 | |||
| 120 | This library is free software; you can redistribute it and/or modify | ||
| 121 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 122 | at your option, any later version of Perl 5 you may have available. | ||
| 123 | |||
| 124 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 125 | |||
| 126 | =cut |