<?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>Technology Bits and Bytes &#187; maqbool</title>
	<atom:link href="http://blogs.circlesource.com/author/maqbool/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.circlesource.com</link>
	<description>CircleSource Technical Talent ShowCase</description>
	<lastBuildDate>Thu, 10 Dec 2009 20:01:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mirth on Windows Server 2008 and SQL server 2008</title>
		<link>http://blogs.circlesource.com/2009/12/11/mirth-on-windows-server-2008-and-sql-server-2008/</link>
		<comments>http://blogs.circlesource.com/2009/12/11/mirth-on-windows-server-2008-and-sql-server-2008/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 20:01:18 +0000</pubDate>
		<dc:creator>maqbool</dc:creator>
				<category><![CDATA[HL7]]></category>
		<category><![CDATA[Mirth]]></category>
		<category><![CDATA[healthcare]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=473</guid>
		<description><![CDATA[If you are in HL7 business and have not looked at Mirth, you must. Kudos to the Mirth team, they have brought out a phenominal product. More I use it more I get amazed with its capabilities. In one of my recent project I had to install Mirth on a brand new Windows server 2008 [...]]]></description>
			<content:encoded><![CDATA[<p>If you are in HL7 business and have not looked at <a href="http://www.mirthcorp.com/">Mirth</a>, you must. Kudos to the Mirth team, they have brought out a phenominal product. More I use it more I get amazed with its capabilities. In one of my recent project I had to install Mirth on a brand new Windows server 2008 running SQL Server 2008. I had not done this kind of implementation earlier and was skeptical as to how it will work. My previous installations on Windows and Linux boxes used MySQL as the internal datbase, this one was different, I had to use SQL Server 2008 for internal database. But it is quite simple, here are the steps:</p>
<p>1. Edit &#8220;C:\Program Files (x86)\Mirth\conf\mirth.properties&#8221;  and make sure the database property is set to sqlserver2005</p>
<p>database=sqlserver2005</p>
<p>2. Login to SQL Server management studio and create an database called &#8220;mirthdb&#8221;</p>
<p>3. Inside SQL Server Management Studio, execute the script  &#8220;C:\Program Files (x86)\Mirth\sqlserver2005-database.sql&#8221; which creates all the necessary tables within mirthdb database.</p>
<p>4. Create an account called &#8220;mirthdb&#8221; and grant necessary priviledges to this account for mirthdb database.</p>
<p>5.  Edit &#8220;C:\Program Files (x86)\Mirth\conf\sqlserver2005-SqlMapConfig.properties&#8221; and add the username and password for the SQL Server user created above.</p>
<p>Thats it, restart the Mirth servcie and login to Mirth. It is as simple as that.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2009/12/11/mirth-on-windows-server-2008-and-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to parse multiple OBX segments in Mirth</title>
		<link>http://blogs.circlesource.com/2009/10/25/how-to-parse-multiple-obx-segments-in-mirth/</link>
		<comments>http://blogs.circlesource.com/2009/10/25/how-to-parse-multiple-obx-segments-in-mirth/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 01:45:05 +0000</pubDate>
		<dc:creator>maqbool</dc:creator>
				<category><![CDATA[HL7]]></category>
		<category><![CDATA[Mirth]]></category>
		<category><![CDATA[healthcare]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=466</guid>
		<description><![CDATA[Often there is a need to parse segments that occur multiple time in an HL7 message like OBX or Ali segments. For example, in an ADT message you will get height and weight in two OBX segments. How do you parse these multiple OBX segments and make the available to the channel map? I am [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">Often there is a need to parse segments that occur multiple time in an HL7 message like OBX or Ali segments. For example, in an ADT message you will get height and weight in two OBX segments. How do you parse these multiple OBX segments and make the available to the channel map? I am going to show you how to parse multiple OBX segments in an ADT message and populate variables in to a channel map so they are accessible for further manipulation in the destination connector&#8230;.</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">For brevity sake I am just going to showing the OBX segments that I am going to parse here instead of the whole message. The following two OBX segments are present in my message:</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 0pt;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;">OBX|1|ST|HT^PATIENT HEIGHT||142.9</span><span style="font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;"></span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 0pt;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;">OBX|2|ST|WT^PATIENT WEIGHT||45.2</span><span style="font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;"></span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">I want to create three variables, one to hold the height, one to hold the weight and one to hold the number of segments we parsed.</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">The first step is to create the channel. I am assuming here you already know how to create a channel. For my channel l have my source as a file reader and a database reader as the destination. I create a transformer for the destination. In the transformer, I paste my sample message in the template section and create a step with step being &#8220;Javascript&#8221;. Then I type the following script as my Java script:</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 0pt;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;">var i=0;<br />
for each (seg in msg..OBX)<br />
{<br />
 <span style="mso-tab-count: 1;">      </span>i++;<br />
 <span style="mso-tab-count: 1;">      </span>var key = seg['OBX.3']['OBX.3.2'].toString()<br />
 <span style="mso-tab-count: 1;">      </span>var obx_val = seg['OBX.5']['OBX.5.1'].toString() ;<br />
 <span style="mso-tab-count: 1;">      </span>if(key.search(/HEIGHT/) != -1)channelMap.put(&#8216;HEIGHT&#8217;, obx_val);<br />
 <span style="mso-tab-count: 1;">      </span>if(key.search(/WEIGHT/) != -1)channelMap.put(&#8216;WEIGHT&#8217;, obx_val);<br />
}<br />
channelMap.put(&#8220;obx_seg_count&#8221;, i);</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';"> This script creates three variables, HEIGHT, WEIGHT and &#8220;obx_seg_count&#8221; that are now available to us in the destination for inserting in to a data base as follows.</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';"> Assuming I have a table called ‘OBX’ and columns called ‘ht’ and ‘wt’, I insert the values in to the table as follows in the detonation tab&#8230;.</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 0pt;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;">expression = &#8220;insert into OBX (ht, wt ) values (&#8216;&#8221; + $(&#8216;HEIGHT&#8217;) + &#8220;&#8216; , &#8216;&#8221; + $(&#8216;WEIGHT&#8217;) + &#8220;&#8216;)&#8221;;<br />
var result = dbConn.executeUpdate(expression);</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">The trick in this is the for each loop above. The expression in the parenthesis is simple XML E4X expression (see <a href="https://wso2.org/project/mashup/0.2/docs/e4xquickstart.html"><span style="color: black; text-decoration: none; text-underline: none;">https://wso2.org/project/mashup/0.2/docs/e4xquickstart.html</span></a> for more details). The other concept to grasp here is to note that the incoming message is available as ‘msg’ object and is just a XML object. Knowing this we can traverse the XML object with E4X and find the elements we are interested in which are OBX elements. The other concept to understand is the ‘maps’ in the Mirth. You need to populate the ‘channel map’ so those variable you create are available throughout your channel. </span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">If you have unknown number of OBX segments in a message, like in a ORU message where you get a report, you can do two things, we can either loop through the way I did up here and concatenate each OBX segment to create on variable called ‘report’ with &lt;cr&gt; at the end and put that in to channel map. Or you could create several variables like following….</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 0pt;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;">var i=0;<br />
for each (seg in msg..OBX)<br />
{<br />
 <span style="mso-tab-count: 1;">      </span>i++;<br />
 <span style="mso-tab-count: 1;">      </span>var key = “report_seg_”+I;<br />
 <span style="mso-tab-count: 1;">      </span>var obx_val = seg['OBX.5']['OBX.5.1'].toString() ;</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 0pt;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt;"> <span style="mso-tab-count: 1;">      </span>channelMap.put(key, obx_val);<br />
}<br />
channelMap.put(&#8220;obx_seg_count&#8221;, i);</span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="line-height: 10.2pt; margin: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: &quot;Georgia&quot;,&quot;serif&quot;; color: black; font-size: 7pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';"> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2009/10/25/how-to-parse-multiple-obx-segments-in-mirth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DICOM File Reader in Ruby</title>
		<link>http://blogs.circlesource.com/2008/12/26/dicom-file-reader-in-ruby/</link>
		<comments>http://blogs.circlesource.com/2008/12/26/dicom-file-reader-in-ruby/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 21:03:31 +0000</pubDate>
		<dc:creator>maqbool</dc:creator>
				<category><![CDATA[DICOM]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[healthcare]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=29</guid>
		<description><![CDATA[Here is a simple DICOM file reader developed in pure ruby. The zip file also contains a driver and a sample image file
We are in the process of developing a toolkit with networking suport. We will post it hre when ready. Enjoy&#8230;.
dicomfilereader
]]></description>
			<content:encoded><![CDATA[<p>Here is a simple DICOM file reader developed in pure ruby. The zip file also contains a driver and a sample image file</p>
<p>We are in the process of developing a toolkit with networking suport. We will post it hre when ready. Enjoy&#8230;.</p>
<p><a rel="attachment wp-att-32" href="http://blogs.circlesource.com/2008/12/26/dicom-file-reader-in-ruby/dicomfilereader/">dicomfilereader</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2008/12/26/dicom-file-reader-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mirth Channel to write to a MySQL database</title>
		<link>http://blogs.circlesource.com/2008/12/24/mirth-channel-to-write-to-a-mysql-database/</link>
		<comments>http://blogs.circlesource.com/2008/12/24/mirth-channel-to-write-to-a-mysql-database/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 01:27:37 +0000</pubDate>
		<dc:creator>maqbool</dc:creator>
				<category><![CDATA[HL7]]></category>
		<category><![CDATA[Mirth]]></category>
		<category><![CDATA[healthcare]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=21</guid>
		<description><![CDATA[Attahced is a screen capture of the process to create a simple channle to write to a MySQL database. The channel reads messages form a text file, parses it and then writes to MySQL database.
create mirth channel movie 
]]></description>
			<content:encoded><![CDATA[<p>Attahced is a screen capture of the process to create a simple channle to write to a MySQL database. The channel reads messages form a text file, parses it and then writes to MySQL database.</p>
<p><a href="http://blogs.circlesource.com/wp-content/uploads/2008/12/mirth1.zip">create mirth channel movie </a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2008/12/24/mirth-channel-to-write-to-a-mysql-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Logging with “Java logging API”</title>
		<link>http://blogs.circlesource.com/2008/12/22/logging-with-%e2%80%9cjava-logging-api%e2%80%9d/</link>
		<comments>http://blogs.circlesource.com/2008/12/22/logging-with-%e2%80%9cjava-logging-api%e2%80%9d/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 04:23:20 +0000</pubDate>
		<dc:creator>maqbool</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=20</guid>
		<description><![CDATA[ 
I wonder why you want to use Java logging API when you can use log4j. Well if you want to or have to use Java’s bundled API for logging, it is not that bad. There are several good articles on the web that go it much more details on this topic then what I [...]]]></description>
			<content:encoded><![CDATA[<p><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves /> <w:TrackFormatting /> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF /> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:SplitPgBreakAndParaMark /> <w:DontVertAlignCellWithSp /> <w:DontBreakConstrainedForcedTables /> <w:DontVertAlignInTxbx /> <w:Word11KerningPairs /> <w:CachedColBalance /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> <m:mathPr> <m:mathFont m:val="Cambria Math" /> <m:brkBin m:val="before" /> <m:brkBinSub m:val="&#45;-" /> <m:smallFrac m:val="off" /> <m:dispDef /> <m:lMargin m:val="0" /> <m:rMargin m:val="0" /> <m:defJc m:val="centerGroup" /> <m:wrapIndent m:val="1440" /> <m:intLim m:val="subSup" /> <m:naryLim m:val="undOvr" /> </m:mathPr></w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"   DefSemiHidden="true" DefQFormat="false" DefPriority="99"   LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Normal" /> <w:LsdException Locked="false" Priority="9" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="heading 1" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9" /> <w:LsdException Locked="false" Priority="39" Name="toc 1" /> <w:LsdException Locked="false" Priority="39" Name="toc 2" /> <w:LsdException Locked="false" Priority="39" Name="toc 3" /> <w:LsdException Locked="false" Priority="39" Name="toc 4" /> <w:LsdException Locked="false" Priority="39" Name="toc 5" /> <w:LsdException Locked="false" Priority="39" Name="toc 6" /> <w:LsdException Locked="false" Priority="39" Name="toc 7" /> <w:LsdException Locked="false" Priority="39" Name="toc 8" /> <w:LsdException Locked="false" Priority="39" Name="toc 9" /> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption" /> <w:LsdException Locked="false" Priority="10" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Title" /> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font" /> <w:LsdException Locked="false" Priority="11" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtitle" /> <w:LsdException Locked="false" Priority="22" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Strong" /> <w:LsdException Locked="false" Priority="20" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Emphasis" /> <w:LsdException Locked="false" Priority="59" SemiHidden="false"    UnhideWhenUsed="false" Name="Table Grid" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text" /> <w:LsdException Locked="false" Priority="1" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="No Spacing" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 1" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 1" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 1" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 1" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision" /> <w:LsdException Locked="false" Priority="34" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="List Paragraph" /> <w:LsdException Locked="false" Priority="29" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Quote" /> <w:LsdException Locked="false" Priority="30" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Quote" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 1" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 1" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 1" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 1" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 1" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 2" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 2" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 2" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 2" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 2" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 2" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 2" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 2" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 3" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 3" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 3" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 3" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 3" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 3" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 3" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 3" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 3" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 4" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 4" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 4" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 4" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 4" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 4" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 4" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 4" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 4" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 5" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 5" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 5" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 5" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 5" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 5" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 5" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 5" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 5" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 6" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 6" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 6" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 6" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 6" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 6" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 6" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 6" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 6" /> <w:LsdException Locked="false" Priority="19" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis" /> <w:LsdException Locked="false" Priority="21" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis" /> <w:LsdException Locked="false" Priority="31" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference" /> <w:LsdException Locked="false" Priority="32" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Reference" /> <w:LsdException Locked="false" Priority="33" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Book Title" /> <w:LsdException Locked="false" Priority="37" Name="Bibliography" /> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading" /> </w:LatentStyles> </xml><![endif]--> <!--[if gte mso 10]></p>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-priority:99;
	mso-style-qformat:yes;
	mso-style-parent:"";
	mso-padding-alt:0in 5.4pt 0in 5.4pt;
	mso-para-margin-top:0in;
	mso-para-margin-right:0in;
	mso-para-margin-bottom:10.0pt;
	mso-para-margin-left:0in;
	line-height:115%;
	mso-pagination:widow-orphan;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;}
</style>
<p><![endif]--></p>
<p class="MsoNormal">I wonder why you want to use Java logging API when you can use log4j. Well if you want to or have to use Java’s bundled API for logging, it is not that bad. There are several good articles on the web that go it much more details on this topic then what I am going to cover her. So, look at the articles at the end of this article for links to some of those detailed articles.</p>
<p class="MsoNormal">So, what am I going to be covering in this article? Just few tricks that I found intriguing and somewhat hard to understand. <span> </span>First of all the <code><span style="font-size: 10pt; line-height: 115%;">logging.properties</span></code> file. This file resides in the JRE directory as <code><span style="font-size: 10pt; line-height: 115%;">lib/logging.properties </span></code><code><span style="font-size: 8pt; line-height: 115%;">(/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib </span></code><span style="font-size: 9pt; line-height: 115%;">for Mac OS users</span>). You can edit this file to control the behavior of the default loggers that get installed when JVM starts. This is definitely is not a good idea. You can copy this file to a user space and then can edit the copy to your liking and load this file as part of your program to control the logging. As I am working on a Mac I will copy the system properties file to my space as <code><span style="font-size: 9pt; line-height: 115%;">/Users/user/LogApi/mylogging.properties</span></code>. Now, I would like to have two types of handlers, the standard Console Handler and a File Handler. I edit the file to install the two handlers. So, make sure that the following line is uncommented in your properties file</p>
<p class="MsoNormal"><code><span style="font-size: 9pt; line-height: 115%;">handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler</span></code></p>
<p class="MsoNormal">I would like to log everything to a file but only the SEVERE and above log entries to my log console. By default, the FileHandler logs everything to the file, so we don’t have to do anything here. To make the ConsoleHandler log only entries higher thatn SEVERE, add line similar to:</p>
<p class="MsoNormal"><code><span style="font-size: 9pt; line-height: 115%;">java.util.logging.ConsoleHandler.level = SEVERE</span></code></p>
<p class="MsoNormal">Configure the FIleHandler handler with the following properties.</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.pattern = /Users/user/LogApi/build/classes/javaapi%u.log</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.limit = 50000</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.count = 1</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.append = true</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">The first line specifies where the logging file is to be placed. Please see (<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/FileHandler.html">http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/FileHandler.html</a>) for other options and detials. The <code><span style="font-size: 9pt; line-height: 115%;">“%u”</span></code> in the above line basically makes the log file rotate through “<code><span style="font-size: 9pt; line-height: 115%;">javaapi0.log</span></code>” and “<code><span style="font-size: 9pt; line-height: 115%;">javaapi1.log</span></code>”. The “<code><span style="font-size: 9pt; line-height: 115%;">count”</span></code> option controls how many of these files the logger cycles through. Also don’t forget to set the <code><span style="font-size: 9pt; line-height: 115%;">append</span></code> mode to <code><span style="font-size: 9pt; line-height: 115%;">true</span></code> as the default this set to false. You are done. A simple program to exercise this is given below:</p>
<p class="MsoNormal">
<p class="MsoNormal">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">package logapi;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">import java.util.logging.*;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">/**</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">* A simple Java program, to understand the Java logging API</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">*/</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">public class Main {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private Logger theLogger = null;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span></span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public static void main(String[] args) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>Main hello = new Main("Hello world!");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>hello.sayHello();</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private String theMessage;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public Main(String message) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theMessage = message;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public void sayHello() {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>try {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>//get my logger for this class.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger = Logger.getLogger(Main.class.getName());</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at INFO level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.info(“info message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at SEVERE level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.severe("severe message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>} catch(Exception e){</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// caught exception, lets log it</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.log(Level.SEVERE,"Caught excepttion, ",e);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">}</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">Once you compile this program using javac to Main.class, you will execute this program with the following command:</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java -Djava.util.logging.config.file=/Users/user/LogApi/mylogging.properties Main</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">You are asking the jvm to load your own logger.properties file with the “-D” switch. This is the output of this program:</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$ java -Djava.util.logging.config.file=/Users/user/LogApi/mylogging.properties Main</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">Dec 21, 2008 8:55:08 PM logapi.Main sayHello</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">SEVERE: severe message...</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">The program after execution also wrote a file called “javaapi0.log” and the contents of this file are:</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$ more javaapi0.log </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">Dec 21, 2008 8:59:32 PM logapi.Main sayHello</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">INFO: info message...</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">Dec 21, 2008 8:59:32 PM logapi.Main sayHello</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">SEVERE: severe message...</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">Notice that on the console we only saw the “SEVERE” message but the log file has both the info and the sever messages.</p>
<p class="MsoNormal">
<p class="MsoNormal">If you don’t want to mess around with the properties files but rather want to control the logging programmatically, we can do that also. Although I advise not doing this as you have no control over the logging once the program is rolled out to the filed except re-compiling. However, I will show you how we can do this programmatically just for understanding sake. I hope you have read the other detail tutorial on the web and know the hierarchy of the loggers and their default behaviour. The basic behavior of the logger is when the message comes to it for logging, it checks with its filter and if the message passes the filter the message is handed over to the installed handlers and then the message is also passed on to the parent logger. The parent logger does the same thing till we hit the “root” logger. The root logger handlers are installed at the start of the JVM through the properties file sitting under JRE/lib directory. What we will do is install our own Console and File handlers and then disable the use of the parent handlers by calling the method “theLogger.setUseParentHandlers(false)”. That way we make sure that the only handlers the message passes through are the handlers we install. Here is the code for this:</p>
<p class="MsoNormal">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">import java.util.logging.*;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">/**</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">* A simple Java program, to understand the Java logging API</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">*/</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">public class Main {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private Logger theLogger = null;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public static void main(String[] args) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>Main hello = new Main("Hello world!");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>hello.sayHello();</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private String theMessage;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public Main(String message) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theMessage = message;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public void sayHello() {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>boolean append = true;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>try {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// create my own Handlers for file and console</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>FileHandler fh = new FileHandler("/Users/user/LogApi/build/classes/javaapi%u.log", 50000, 2, append);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>//ConsoleHandler only if we need to log to Console</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>ConsoleHandler ch = new ConsoleHandler();</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>//get my logger for this class.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger = Logger.getLogger(Main.class.getName());</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// the logging gets bubbled up to the parent and the parent default</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// handlers are used. Lets disable the use of the parent Handlers.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.setUseParentHandlers(false);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// set the formatting for the log file to the simple</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>fh.setFormatter(new java.util.logging.SimpleFormatter());</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// install our own handlers in to the logger.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.addHandler(fh);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// ConsoleHandler Seetings only if we want to log to console.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>ch.setLevel(Level.SEVERE);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.addHandler(ch);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at INFO level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.info("info message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at SEVERE level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.severe("severe message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>} catch(Exception e){</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// caught exception, lets log it</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.log(Level.SEVERE,"Caught excepttion, ",e);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal">Compile this to Main.class and execute to get the same effect as the previous program.</p>
<p class="MsoNormal">Finally, the reason I had to deal with this is, we were working on a Java program to parse some files on a regular interval (every 2 minutes). This program got installed under cron. As you know, under cron anything that is written to the stdout or stderr gets emailed to the user under which the program is running. If you start logging a lot of messages to stdout/stderr (the console) you will get a lots of messages flooding your inbox. Worst yet, the /var/mail/user file may continue to grow and fill up the whole space if the program is left running for a long time without cleaning the messages. If we selectively log messages to the console, then you get an e-mail message for the critical messages that you want to see any ways. <span> </span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><a href="http://www.onjava.com/pub/a/onjava/2002/06/19/log.html">http://www.onjava.com/pub/a/onjava/2002/06/19/log.html</a></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><a href="http://www.crazysquirrel.com/computing/java/logging.jspx">http://www.crazysquirrel.com/computing/java/logging.jspx</a></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><a href="http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html">http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html</a></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;">
<p class="MsoNormal">
<p class="MsoNormal">
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2008/12/22/logging-with-%e2%80%9cjava-logging-api%e2%80%9d/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
