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
|
#-*- perl -*-
#
# Copyright (C) 2000 Ken'ichi Fukamachi
#
# $FML: Log.pm,v 1.11 2001/05/27 14:27:53 fukachan Exp $
#
package FML::Log;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(Log LogWarn LogError);
use strict;
use Carp;
use FML::Config;
use FML::Date;
use FML::Credential;
=head1 NAME
FML::Log - several interfaces to open several files
=head1 SYNOPSIS
To import Log(),
use FML::Log qw(Log LogWarn LogError);
&Log( $log_message );
or specify arguments in the hash reference
use FML::Log qw(Log LogWarn LogError);
&Log( $log_message , {
log_file => $log_file,
priority => $priority,
facility => $facility,
level => $level,
});
=head1 DESCRIPTION
FML::Log.pm contains several interfaces to write log,
for example, log files, syslog() (not yet implemented).
=head2 Log( $message [, $args])
The required argument is the message to log.
You can specify C<log_file>, C<facility> and C<level> as an optional.
$args = {
log_file => $log_file,
priority => $priority,
facility => $facility,
level => $level,
};
This routine depends on C<FML::Config> and C<FML::Credential>.
$config->{ log_format_type } defines the format sytle.
C<sender> to log is taken from C<FML::Credential> object.
Key C<log_format_type> changes the log format.
By default our log format is same as one of fml 4.0.
=head2 LogWarn( $message [, $args])
same as Log("warn: $message", $args);
=head2 LogError( $message [, $args])
same as Log("error: $message", $args);
=cut
sub Log
{
my ($mesg, $args) = @_;
my $config = new FML::Config;
# parse arguments
my $log_file = $args->{ log_file };
my $priority = $args->{ priority };
my $facility = $args->{ facility };
my $level = $args->{ level };
# invalid calling
$mesg || return undef ;
# reference to "date" object
my $rdate = new FML::Date;
# open the $file by using FileHandle.pm
use FileHandle;
# When the second argument is not defined, use the default log_file.
my $style = $config->{ log_format_type } || 'fml4_compatible';
my $file = $log_file || $config->{ log_file } || '/dev/stderr';
my $fh = new FileHandle ">> $file";
my $sender = FML::Credential->sender;
if (defined $fh) {
if ($style eq 'fml4_compatible') {
print $fh $rdate->{'log_file_style'}, " ", $mesg;
print $fh " ($sender)" if defined $sender;
print $fh "\n";
}
else {
my $name = $0; $name =~ s@.*/@@;
my $iam = $name."[". $config->{ _pid } ."]";
print $fh $rdate->{'log_file_style'}, " ", $iam, " ", $mesg;
print $fh "\n";
}
}
else {
croak("cannot open $file");
}
}
sub LogWarn
{
my ($mesg, $args) = @_;
Log("warn: ".$mesg, $args);
}
sub LogError
{
my ($mesg, $args) = @_;
Log("error: ".$mesg, $args);
}
=head1 SEE ALSO
L<FML::Date>,
L<FML::Config>,
L<FML::Credential>,
=head1 AUTHOR
Ken'ichi Fukamachi <F<fukachan@fml.org>>
=head1 COPYRIGHT
Copyright (C) 2000,2001 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::Log appeared in fml5 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|