1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::System::Log::SysLog;
10
11use strict;
12use warnings;
13
14use Sys::Syslog qw();
15
16our @ObjectDependencies = (
17    'Kernel::Config',
18    'Kernel::System::Encode',
19);
20
21sub new {
22    my ( $Type, %Param ) = @_;
23
24    # allocate new hash for object
25    my $Self = {};
26    bless( $Self, $Type );
27
28    # set syslog facility
29    $Self->{SysLogFacility} = $Kernel::OM->Get('Kernel::Config')->Get('LogModule::SysLog::Facility') || 'user';
30
31    return $Self;
32}
33
34sub Log {
35    my ( $Self, %Param ) = @_;
36
37    # get needed objects
38    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
39    my $EncodeObject = $Kernel::OM->Get('Kernel::System::Encode');
40
41    # prepare data for byte output
42    if ( $ConfigObject->Get('LogModule::SysLog::Charset') =~ m/^utf-?8$/ ) {
43        $EncodeObject->EncodeOutput( \$Param{Message} );
44    }
45    else {
46        $Param{Message} = $EncodeObject->Convert(
47            Text  => $Param{Message},
48            From  => 'utf8',
49            To    => $ConfigObject->Get('LogModule::SysLog::Charset') || 'iso-8859-15',
50            Force => 1,
51        );
52    }
53
54    Sys::Syslog::openlog( $Param{LogPrefix}, 'cons,pid', $Self->{SysLogFacility} );
55
56    if ( lc $Param{Priority} eq 'debug' ) {
57        Sys::Syslog::syslog( 'debug', "[Debug][$Param{Module}][$Param{Line}] $Param{Message}" );
58    }
59    elsif ( lc $Param{Priority} eq 'info' ) {
60        Sys::Syslog::syslog( 'info', "[Info][$Param{Module}] $Param{Message}" );
61    }
62    elsif ( lc $Param{Priority} eq 'notice' ) {
63        Sys::Syslog::syslog( 'notice', "[Notice][$Param{Module}] $Param{Message}" );
64    }
65    elsif ( lc $Param{Priority} eq 'error' ) {
66        Sys::Syslog::syslog( 'err', "[Error][$Param{Module}][Line:$Param{Line}]: $Param{Message}" );
67    }
68    else {
69
70        # print error messages to STDERR
71        print STDERR
72            "[Error][$Param{Module}] Priority: '$Param{Priority}' not defined! Message: $Param{Message}\n";
73
74        # and of course to syslog
75        Sys::Syslog::syslog(
76            'err',
77            "[Error][$Param{Module}] Priority: '$Param{Priority}' not defined! Message: $Param{Message}"
78        );
79    }
80
81    Sys::Syslog::closelog();
82
83    return;
84}
85
861;
87