1package Log::Log4perl::JavaMap; 2 3use Carp; 4use strict; 5 6use constant _INTERNAL_DEBUG => 0; 7 8our %translate = ( 9 'org.apache.log4j.ConsoleAppender' => 10 'Log::Log4perl::JavaMap::ConsoleAppender', 11 'org.apache.log4j.FileAppender' => 12 'Log::Log4perl::JavaMap::FileAppender', 13 'org.apache.log4j.RollingFileAppender' => 14 'Log::Log4perl::JavaMap::RollingFileAppender', 15 'org.apache.log4j.TestBuffer' => 16 'Log::Log4perl::JavaMap::TestBuffer', 17 'org.apache.log4j.jdbc.JDBCAppender' => 18 'Log::Log4perl::JavaMap::JDBCAppender', 19 'org.apache.log4j.SyslogAppender' => 20 'Log::Log4perl::JavaMap::SyslogAppender', 21 'org.apache.log4j.NTEventLogAppender' => 22 'Log::Log4perl::JavaMap::NTEventLogAppender', 23); 24 25our %user_defined; 26 27sub get { 28 my ($appender_name, $appender_data) = @_; 29 30 print "Trying to map $appender_name\n" if _INTERNAL_DEBUG; 31 32 $appender_data->{value} || 33 die "ERROR: you didn't tell me how to implement your appender " . 34 "'$appender_name'"; 35 36 my $perl_class = $translate{$appender_data->{value}} || 37 $user_defined{$appender_data->{value}} || 38 die "ERROR: I don't know how to make a '$appender_data->{value}' " . 39 "to implement your appender '$appender_name', that's not a " . 40 "supported class\n"; 41 eval { 42 eval "require $perl_class"; #see 'perldoc -f require' for why two evals 43 die $@ if $@; 44 }; 45 $@ and die "ERROR: trying to set appender for $appender_name to " . 46 "$appender_data->{value} using $perl_class failed\n$@ \n"; 47 48 my $app = $perl_class->new($appender_name, $appender_data); 49 return $app; 50} 51 52#an external api to the two hashes 53sub translate { 54 my $java_class = shift; 55 56 return $translate{$java_class} || 57 $user_defined{$java_class}; 58} 59 601; 61 62 63=head1 NAME 64 65Log::Log4perl::JavaMap - maps java log4j appenders to Log::Dispatch classes 66 67=head1 SYNOPSIS 68 69 ############################### 70 log4j.appender.FileAppndr1 = org.apache.log4j.FileAppender 71 log4j.appender.FileAppndr1.File = /var/log/onetime.log 72 log4j.appender.FileAppndr1.Append = false 73 74 log4j.appender.FileAppndr1.layout = org.apache.log4j.PatternLayout 75 log4j.appender.FileAppndr1.layout.ConversionPattern=%d %4r [%t] %-5p %c %x - %m%n 76 ############################### 77 78 79=head1 DESCRIPTION 80 81If somebody wants to create an appender called C<org.apache.log4j.ConsoleAppender>, 82we want to translate it to Log::Dispatch::Screen, and then translate 83the log4j options into Log::Dispatch parameters.. 84 85=head2 What's Implemented 86 87(Note that you can always use the Log::Dispatch::* module. By 'implemented' 88I mean having a translation class that translates log4j options into 89the Log::Dispatch options so you can use log4j rather than log4perl 90syntax in your config file.) 91 92Here's the list of appenders I see on the current (6/2002) log4j site. 93 94These are implemented 95 96 ConsoleAppender - Log::Dispatch::Screen 97 FileAppender - Log::Dispatch::File 98 RollingFileAppender - Log::Dispatch::FileRotate (by Mark Pfeiffer) 99 JDBCAppender - Log::Log4perl::Appender::DBI 100 SyslogAppender - Log::Dispatch::Syslog 101 NTEventLogAppender - Log::Dispatch::Win32EventLog 102 103 104These should/will/might be implemented 105 106 DailyRollingFileAppender - 107 SMTPAppender - Log::Dispatch::Email::MailSender 108 109 110These might be implemented but they don't have corresponding classes 111in Log::Dispatch (yet): 112 113 NullAppender 114 TelnetAppender 115 116These might be simulated 117 118 LF5Appender - use Tk? 119 ExternallyRolledFileAppender - catch a HUP instead? 120 121These will probably not be implemented 122 123 AsyncAppender 124 JMSAppender 125 SocketAppender - (ships a serialized LoggingEvent to the server side) 126 SocketHubAppender 127 128=head1 ROLL YOUR OWN 129 130Let's say you've in a mixed Java/Perl enviroment and you've 131come up with some custom Java appender with behavior you want to 132use in both worlds, C<myorg.customAppender>. You write a 133Perl appender with the same behavior C<Myorg::CustomAppender>. You 134want to use one config file accross both applications, so the 135config file will have to say 'myorg.customAppender'. But 136the mapping from C<myorg.customAppender> to C<Myorg::CustomAppender> 137isn't in this JavaMap class, so what do you do? 138 139In your Perl code, before you call Log::Log4perl::init(), do this: 140 141 $Log::Log4perl::JavaMap::user_defined{'myorg.customAppender'} = 142 'Myorg::CustomAppender'; 143 144and you can use 'myorg.customAppender' in your config file with 145impunity. 146 147=head1 SEE ALSO 148 149http://jakarta.apache.org/log4j/docs/ 150 151=head1 LICENSE 152 153Copyright 2002-2012 by Mike Schilli E<lt>m@perlmeister.comE<gt> 154and Kevin Goess E<lt>cpan@goess.orgE<gt>. 155 156This library is free software; you can redistribute it and/or modify 157it under the same terms as Perl itself. 158 159=head1 AUTHOR 160 161Please contribute patches to the project on Github: 162 163 http://github.com/mschilli/log4perl 164 165Send bug reports or requests for enhancements to the authors via our 166 167MAILING LIST (questions, bug reports, suggestions/patches): 168log4perl-devel@lists.sourceforge.net 169 170Authors (please contact them via the list above, not directly): 171Mike Schilli <m@perlmeister.com>, 172Kevin Goess <cpan@goess.org> 173 174Contributors (in alphabetical order): 175Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton 176Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony 177Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy 178Grundman, Paul Harrington, David Hull, Robert Jacobson, Jason Kohles, 179Jeff Macdonald, Markus Peter, Brett Rann, Peter Rabbitson, Erik 180Selberg, Aaron Straup Cope, Lars Thegler, David Viner, Mac Yang. 181 182