<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ralph's TechBlog &#187; Commons Logging</title>
	<atom:link href="http://techblog.ralph-schuster.eu/tag/commons-logging/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.ralph-schuster.eu</link>
	<description>Technical stuff for IT experts</description>
	<lastBuildDate>Tue, 27 Dec 2011 09:38:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Configuring logging in a Java web application</title>
		<link>http://techblog.ralph-schuster.eu/2007/12/04/configuring-logging-in-a-java-web-application/</link>
		<comments>http://techblog.ralph-schuster.eu/2007/12/04/configuring-logging-in-a-java-web-application/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 10:56:31 +0000</pubDate>
		<dc:creator>Ralph</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Commons Logging]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Locale]]></category>
		<category><![CDATA[Servlet]]></category>

		<guid isPermaLink="false">http://techblog.ralph-schuster.eu/2007/12/04/configuring-logging-in-a-java-web-application/</guid>
		<description><![CDATA[Here is a short HOWTO for making some initial configuring when using Commons logging or log4j. Additionally this post will describe how to set the default locale in a servlet environment. First you will need to create a new class derived from HttpServlet. 1 2 3 4 5 6 7 8 9 10 11 12 [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.ralph-schuster.eu%2F2007%2F12%2F04%2Fconfiguring-logging-in-a-java-web-application%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.ralph-schuster.eu%2F2007%2F12%2F04%2Fconfiguring-logging-in-a-java-web-application%2F&amp;source=TechnicalG&amp;style=compact&amp;hashtags=Commons+Logging,JSP,Locale,Servlet&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Here is a short HOWTO for making some initial configuring when using Commons logging or log4j. Additionally this post will describe how to set the default locale in a servlet environment.</p>
<p>First you will need to create a new class derived from HttpServlet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">mypackage</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Locale</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.LogFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.PropertyConfigurator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Initializes log4j so it reads its configuration from WEB-INF directory and sets default locale.
 * @author Ralph Schuster
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LogConfiguratorServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Serial ID.
    */</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>5756062055681369027L<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DEFAULT_FILE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WEB-INF/log4j.properties&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DEFAULT_LOCALE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;en_US&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// You can use &quot;en&quot;, too.</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Initializes log4j and sets default locale.
    */</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Do the log4j configuration</span>
      <span style="color: #003399;">String</span> prefix <span style="color: #339933;">=</span>  getServletContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRealPath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">String</span> file <span style="color: #339933;">=</span> getInitParameter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config-file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// if the config-file is not set, then no point in trying</span>
      <span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>file <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         s <span style="color: #339933;">=</span> prefix<span style="color: #339933;">+</span>file<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
         s <span style="color: #339933;">=</span> prefix<span style="color: #339933;">+</span>DEFAULT_FILE<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      PropertyConfigurator.<span style="color: #006633;">configure</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;log4j configuration file: &quot;</span><span style="color: #339933;">+</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Do the locale configuration</span>
      s <span style="color: #339933;">=</span> getInitParameter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;locale&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> s <span style="color: #339933;">=</span> DEFAULT_LOCALE<span style="color: #339933;">;</span>
      <span style="color: #003399;">Locale</span> available<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003399;">Locale</span>.<span style="color: #006633;">getAvailableLocales</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>available.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>available<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Locale</span>.<span style="color: #006633;">setDefault</span><span style="color: #009900;">&#40;</span>available<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Default locale set to: &quot;</span><span style="color: #339933;">+</span><span style="color: #003399;">Locale</span>.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Does nothing.
    */</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You then need to deploy the class in your servlet container and adjust the web.xml file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #ddbb00;">&amp;lt;</span>servlet<span style="color: #ddbb00;">&amp;gt;</span>
      <span style="color: #ddbb00;">&amp;lt;</span>/servlet-name<span style="color: #ddbb00;">&amp;gt;</span>log4j-init<span style="color: #ddbb00;">&amp;lt;</span>/servlet-name<span style="color: #ddbb00;">&amp;gt;</span>
      <span style="color: #ddbb00;">&amp;lt;</span>servlet-class<span style="color: #ddbb00;">&amp;gt;</span>mypackage.LogConfiguratorServlet<span style="color: #ddbb00;">&amp;lt;</span>/servlet-class<span style="color: #ddbb00;">&amp;gt;</span>
&nbsp;
      <span style="color: #ddbb00;">&amp;lt;</span>init-param<span style="color: #ddbb00;">&amp;gt;</span>
         <span style="color: #ddbb00;">&amp;lt;</span>param-name<span style="color: #ddbb00;">&amp;gt;</span>config-file<span style="color: #ddbb00;">&amp;lt;</span>/param-name<span style="color: #ddbb00;">&amp;gt;</span>
         <span style="color: #ddbb00;">&amp;lt;</span>param-value<span style="color: #ddbb00;">&amp;gt;</span>WEB-INF/log4j.properties<span style="color: #ddbb00;">&amp;lt;</span>/param-value<span style="color: #ddbb00;">&amp;gt;</span>
      <span style="color: #ddbb00;">&amp;lt;</span>/init-param<span style="color: #ddbb00;">&amp;gt;</span>
      <span style="color: #ddbb00;">&amp;lt;</span>init-param<span style="color: #ddbb00;">&amp;gt;</span>
         <span style="color: #ddbb00;">&amp;lt;</span>param-name<span style="color: #ddbb00;">&amp;gt;</span>locale<span style="color: #ddbb00;">&amp;lt;</span>/param-name<span style="color: #ddbb00;">&amp;gt;</span>
         <span style="color: #ddbb00;">&amp;lt;</span>param-value<span style="color: #ddbb00;">&amp;gt;</span>de_DE<span style="color: #ddbb00;">&amp;lt;</span>/param-value<span style="color: #ddbb00;">&amp;gt;</span>
      <span style="color: #ddbb00;">&amp;lt;</span>/init-param<span style="color: #ddbb00;">&amp;gt;</span>
&nbsp;
      <span style="color: #ddbb00;">&amp;lt;</span>load-on-startup<span style="color: #ddbb00;">&amp;gt;</span>1<span style="color: #ddbb00;">&amp;lt;</span>/load-on-startup<span style="color: #ddbb00;">&amp;gt;</span>
   <span style="color: #ddbb00;">&amp;lt;</span>/servlet<span style="color: #ddbb00;">&amp;gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ralph-schuster.eu/2007/12/04/configuring-logging-in-a-java-web-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

