|
Hello,
maybe you could use a Log package or a class like this (instead of
System.out.println):
package com.sisa.zm90.internet;
import com.ibm.as400.access.*;
import java.util.*;
import java.io.*;
import java.text.*;
/**
* Log messages to the console or to the file system
* log type is retrievd from the ini file
*
* Creation date: (20.02.2002)
* @author: Franco Biaggi
*/
public class Logger {
protected static Logger singleton;
static AS400 as400 = null;
static DateFormat df = java.text.DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.MEDIUM, Locale.getDefault());
Date actualDate = null;
SequentialFile as400File = null;
RecordFormat[] recordFormat = null;
Record record = null;
String lock = "";
/**
* Constructor for Logger, not accessible
*/
Logger() {
super();
}
/**
* Return a uniqe instance of this logger in this JVM
*
* @return Logger
*/
public static Logger singleton() {
if ( singleton == null )
singleton = new Logger();
return singleton;
}
/**
* Log a text string
*
* @param logText Write a log text string
*/
public void log( String logText ) {
if ( IniFileManager.getLoggerType().equals( "JAVA_CONSOLE" ) ) {
logToConsole( logText );
}
if ( IniFileManager.getLoggerType().equals( "FILE_SYSTEM" ) ) {
logToFileAS400( logText );
}
}
/**
* Log an exception
*
* @param logException Throwable
*/
public void log( Throwable logException ) {
if ( IniFileManager.getLoggerType().equals( "JAVA_CONSOLE" ) ) {
logExceptionToConsole( logException );
}
if ( IniFileManager.getLoggerType().equals( "FILE_SYSTEM" ) ) {
logExceptioToFileAS400( logException );
}
}
/**
* Write log to the console
*
* @param logText String
*/
private void logToConsole( String logText ) {
actualDate = new Date();
System.out.println( df.format( actualDate ) + " " + logText );
}
/**
* Write Stack trace to the console
*
* @param logException Throwable
*/
private void logExceptionToConsole( Throwable logException ) {
actualDate = new Date();
System.out.println( df.format( actualDate ) + " " +
logException.getMessage() );
// Make a buffer to store the stack trace
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PrintStream stackInfo = new PrintStream( buffer );
logException.printStackTrace( stackInfo );
// Create the tokenizer to get each line
StringTokenizer tok = new StringTokenizer( buffer.toString(),
"\n" );
while( tok.hasMoreElements() ) {
System.out.println( df.format( actualDate ) + " " + (
( String ) tok.nextElement() ).trim() );
}
}
/**
* Write log to the AS400 file system
*
* @param logText String
*/
private void logToFileAS400( String logText ) {
setAS400Object();
try {
synchronized ( lock ) {
as400File.open(
AS400File.WRITE_ALLOW_SHARED_WRITE_LOCK, 1, AS400File.COMMIT_LOCK_LEVEL_NONE );
actualDate = new Date();
String rcd = df.format( actualDate ) + " " +
logText;
record.setField( 0, rcd );
as400File.write( record );
as400File.close();
}
} catch (Throwable th) {
System.out.println( "[Error] " + getClass() + " Error
writing log to the AS/400." );
System.out.println( th.getMessage() );
th.printStackTrace();
}
}
/**
* Write Stack trace to the AS400 file system
*
* @param logText String
*/
private void logExceptioToFileAS400( Throwable logException ) {
setAS400Object();
try {
synchronized ( lock ) {
as400File.open(
AS400File.WRITE_ALLOW_SHARED_WRITE_LOCK, 1, AS400File.COMMIT_LOCK_LEVEL_NONE );
actualDate = new Date();
String rcd = df.format( actualDate ) + " " +
logException.getMessage();
record.setField( 0, rcd );
as400File.write( record );
// Make a buffer to store the stack trace
ByteArrayOutputStream buffer = new
ByteArrayOutputStream();
PrintStream stackInfo = new PrintStream( buffer
);
logException.printStackTrace( stackInfo );
// Create the tokenizer to get each line
StringTokenizer tok = new StringTokenizer(
buffer.toString(), "\n" );
while( tok.hasMoreElements() ) {
rcd = df.format( actualDate ) + " " +
( ( String ) tok.nextElement() ).trim();
record.setField( 0, rcd );
as400File.write( record );
}
as400File.close();
}
} catch (Throwable th) {
System.out.println( "[Error] " + getClass() + " Error
writing log to the AS/400." );
System.out.println( th.getMessage() );
th.printStackTrace();
}
}
/**
* Set the AS/400 object
*/
void setAS400Object() {
try {
if ( as400 == null ) {
as400 = new AS400();
as400.setSystemName(
IniFileManager.getAS400SystemName() );
as400.setUserId(
IniFileManager.getAS400UserId() );
as400.setPassword(
IniFileManager.getAS400Password() );
as400.setGuiAvailable( false );
as400.connectService( AS400.RECORDACCESS );
}
if ( as400File == null ) {
as400File = new SequentialFile( as400,
IniFileManager.getLoggerFileName() );
AS400FileRecordDescription recordDescription =
new AS400FileRecordDescription( as400, IniFileManager.getLoggerFileName() );
recordFormat =
recordDescription.retrieveRecordFormat();
as400File.setRecordFormat( recordFormat[ 0 ] );
record = new Record( recordFormat[ 0 ] );
}
} catch (Throwable th) {
System.out.println( "[Error] " + getClass() + " Error
connecting to the AS/400. Exiting..." );
System.out.println( th.getMessage() );
th.printStackTrace();
System.exit( 1 );
}
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Franco Biaggi">
<meta name="GENERATOR" content="Franco Biaggi">
</head>
<body>
<p> </p>
<table BORDER="1" COLS="1" WIDTH="452" BGCOLOR="#FFFF00">
<tr>
<td width="446"><font face="Comic Sans MS"><small><small><br>
<font color="#3333FF"><a href="http://www.ticino.com/usr/fbiaggi/">Franco
Biaggi</a></font><small><br>
<font color="#3333FF"><a href="http://www.sisa.ch">SISA Studio Informatica
SA</a></font><small><small><small> <br>
</small></small></small><font color="#3333FF">R&D
Services</font><small><small><small>
<br>
</small></small></small><font color="#3333FF">Via Carvina
1</font><small><small><small> <br>
</small></small></small><font color="#3333FF">CH-6807
Taverne</font><small><small><small> <br>
</small></small></small><font color="#3333FF">Efax +1 801 705
2839</font></small></small></small><br>
<br>
</font></td>
</tr>
</table>
</body>
</html>
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.