You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Amandeep Midha <Am...@itsprojects.com> on 2003/05/16 15:22:02 UTC

Log4J - Mail

Any of you could u pls send me sample of how to send mail using log4j
(using config XML) under if priority error or more

thanks in advance

best regards,
Amandeep


-----------------------------------------
Amandeep Midha (CHARMIE)
IT Solutions (India) Pvt. Ltd.
No. 17, South End Road,
Basavangudi
Bangalore - 560 004. India
TEL : 91-80-6657180 EXT: 2116
CELL: +91 9886148227
FAX: 91-80-6655755
-----------------------------------------
"Never Fear Shadows ;They only mean there is a light shining somewhere
nearby ...."


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Re: Log4J - Mail

Posted by Nathan Pitts <jp...@tahc.state.tx.us>.
I use the log4j.properties method of setting up email upon error 
conditions because in my situation, it is the easiest way.  I just have 
the properties file in my class path for my web application.   It 
should be relatively easy to use the xml file format, however...The 
ways that you initialize log4j will vary depending on your environment 
-- is this a webapp, standalone app, etc....The properties that need to 
be set whether it's an xml config file or a properties will be the 
same.  Here's my log4j.properties file that configures log4j to send me 
an email whenever an error or fatal error occurs:

hope this helps!
--nathan



# The root logging configuration
log4j.rootCategory=WARN, mail, rolling

# Custom package log levels. For now, I want extra logging output for
# my own classes and related tools.
log4j.logger.tahc.hris=DEBUG
log4j.logger.net.sf.hibernate=INFO
log4j.logger.org.apache.commons=WARN
log4j.logger.org.apache.struts=WARN


# Logging configuration for stdout (will go to catalina.out on tomcat)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p] %d{DATE} %c - %m%n

# Logging configuration for the hris.log file
log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.File=hris.log
log4j.appender.rolling.MaxFileSize=1MB
log4j.appender.rolling.MaxBackupIndex=3
log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling.layout.ConversionPattern=[%p] %d{DATE} %c - %m%n

# Configuration for receiving e-mails when ERROR messages occur.
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.To=jpitts@tahc.state.tx.us
log4j.appender.mail.From=jpitts@tahc.state.tx.us
log4j.appender.mail.SMTPHost=tahc.state.tx.us
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.BufferSize=1
log4j.appender.mail.Subject=HRIS Application Error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=[%p] %d{DATE} %c - %m%n




More info that might be helpful:

Default Initialization Procedure

The log4j library does not make any assumptions about its environment. 
In particular, there are no default log4j appenders. Under certain 
well-defined circumstances however, the static inializer of the Logger 
class will attempt to automatically configure log4j. The Java language 
guarantees that the static initializer of a class is called once and 
only once during the loading of a class into memory. It is important to 
remember that different classloaders may load distinct copies of the 
same class. These copies of the same class are considered as totally 
unrelated by the JVM.

The default initialization is very useful in environments where the 
exact entry point to the application depends on the runtime 
environment. For example, the same application can be used as a 
stand-alone application, as an applet, or as a servlet under the 
control of a web-server.

The exact default initialization algorithm is defined as follows:

1.	Setting the log4j.defaultInitOverride system property to any other 
value then "false" will cause log4j to skip the default initialization 
procedure (this procedure).

2.	Set the resource string variable to the value of the 
log4j.configuration system property. The preferred way to specify the 
default initialization file is through the log4j.configuration system 
property. In case the system property log4j.configuration is not 
defined, then set the string variable resource to its default value 
"log4j.properties".

3.	Attempt to convert the resource variable to a URL.

4.	If the resource variable cannot be converted to a URL, for example 
due to a MalformedURLException, then search for the resource from the 
classpath by calling 
org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) 
which returns a URL. Note that the string "log4j.properties" 
constitutes a malformed URL.

See Loader.getResource(java.lang.String) for the list of searched 
locations.

5.	If no URL could not be found, abort default initialization. 
Otherwise, configure log4j from the URL.

The PropertyConfigurator will be used to parse the URL to configure 
log4j unless the URL ends with the ".xml" extension, in which case the 
DOMConfigurator will be used. You can optionaly specify a custom 
configurator. The value of the log4j.configuratorClass system property 
is taken as the fully qualified class name of your custom configurator. 
The custom configurator you specify must implement the Configurator 
interface.

Example Configurations

Default Initialization under Tomcat

The default log4j initialization is particularly useful in web-server 
environments. Under Tomcat 3.x and 4.x, you should place the 
log4j.properties under the WEB-INF/classes directory of your 
web-applications. Log4j will find the properties file and initialize 
itself. This is easy to do and it works.

You can also choose to set the system property log4j.configuration 
before starting Tomcat. For Tomcat 3.x The TOMCAT_OPTS environment 
variable is used to set command line options. For Tomcat 4.0, set the 
CATALINA_OPTS environment variable instead of TOMCAT_OPTS.

Example 1

The Unix shell command


    export TOMCAT_OPTS="-Dlog4j.configuration=foobar.txt"

tells log4j to use the file foobar.txt as the default configuration 
file. This file should be place under the WEB-INF/classes directory of 
your web-application. The file will be read using the 
PropertyConfigurator. Each web-application will use a different default 
configuration file because each file is relative to a web-application.

Example 2

The Unix shell command


    export TOMCAT_OPTS="-Dlog4j.debug -Dlog4j.configuration=foobar.xml"

tells log4j to output log4j-internal debugging information and to use 
the file foobar.xml as the default configuration file. This file should 
be place under the WEB-INF/classes directory of your web-application. 
Since the file ends with a .xml extension, it will read using the 
DOMConfigurator. Each web-application will use a different default 
configuration file because each file is relative to a web-application.

Example 3

The Windows shell command


    set TOMCAT_OPTS=-Dlog4j.configuration=foobar.lcf 
-Dlog4j.configuratorClass=com.foo.BarConfigurator

tells log4j to use the file foobar.lcf as the default configuration 
file. This file should be place under the WEB-INF/classes directory of 
your web-application. Due to the definition of the 
log4j.configuratorClass system property, the file will be read using 
the com.foo.BarConfigurator custom configurator. Each web-application 
will use a different default configuration file because each file is 
relative to a web-application.

Example 4

The Windows shell command


    set TOMCAT_OPTS=-Dlog4j.configuration=file:/c:/foobar.lcf
tells log4j to use the file c:\foobar.lcf as the default configuration 
file. The configuration file is fully specified by the URL 
file:/c:/foobar.lcf. Thus, the same configuration file will be used for 
all web-applications.

Different web-applications will load the log4j classes through their 
respective classloaderss. Thus, each image of the log4j environment 
will act independetly and without any mutual synchronization. For 
example, FileAppenders defined exactly the same way in multiple 
web-application configurations will all attempt to write the same file. 
The results are likely to be less than satisfactory. You must make sure 
that log4j configurations of different web-applications do not use the 
same underlying system resource.

Initialization servlet

It is also possible to use a special servlet for log4j initialization. 
Here is an example,


package com.foo;

import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;

public class Log4jInit extends HttpServlet {

   public
   void init() {
     String prefix =  getServletContext().getRealPath("/");
     String file = getInitParameter("log4j-init-file");
     // if the log4j-init-file is not set, then no point in trying
     if(file != null) {
       PropertyConfigurator.configure(prefix+file);
     }
   }

   public
   void doGet(HttpServletRequest req, HttpServletResponse res) {
   }
}


Define the following servlet in the web.xml file for your 
web-application.


   <servlet>
     <servlet-name>log4j-init</servlet-name>
     <servlet-class>com.foo.Log4jInit</servlet-class>

     <init-param>
       <param-name>log4j-init-file</param-name>
       <param-value>WEB-INF/classes/log4j.lcf</param-value>
     </init-param>

     <load-on-startup>1</load-on-startup>
   </servlet>


Writing an initialization servlet is the most flexible way for 
initializing log4j. There are no constraints on the code you can place 
in the init() method of the servlet.









On Friday, May 16, 2003, at 08:22 AM, Amandeep Midha wrote:

>
> Any of you could u pls send me sample of how to send mail using log4j
> (using config XML) under if priority error or more
>
> thanks in advance
>
> best regards,
> Amandeep
>
>
> -----------------------------------------
> Amandeep Midha (CHARMIE)
> IT Solutions (India) Pvt. Ltd.
> No. 17, South End Road,
> Basavangudi
> Bangalore - 560 004. India
> TEL : 91-80-6657180 EXT: 2116
> CELL: +91 9886148227
> FAX: 91-80-6655755
> -----------------------------------------
> "Never Fear Shadows ;They only mean there is a light shining somewhere
> nearby ...."
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: log4j-user-help@jakarta.apache.org
>
>



=============================
Nathan Pitts
Programmer Analyst
Texas Animal Health Commission
=============================