WebSphere如何将计时跟踪记录到单独的日志中? [英] WebSphere how to log timing traces to separate log?

查看:105
本文介绍了WebSphere如何将计时跟踪记录到单独的日志中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在WebSphere应用程序中进行了很多跟踪日志记录,并且希望将一些计时信息分隔在单独的日志文件中.

We do a lot of trace logging in our WebSphere application and would like to separate some timing information in a separate log file.

通常我们导入:

import java.util.logging.Level;
import java.util.logging.Logger;

然后声明:

 private static final Logger logger = Logger.getLogger(Myclass.class.getName()); 

然后登录:

 logger.info("now logging...");

最简单的方法是什么?

推荐答案

您可以使用Apache日志记录服务在Websphere Application Server外部创建日志.从此处.这是我们使用过的jar的稳定版本.将此jar添加到您的应用程序构建路径/类路径/库.在 src 文件夹(空包)下添加 log4j.properties 文件. log4j.properties将包含以下几行:

You can use Apache logging services to create logs outside Websphere Application Server. Download log4j jar from here. This is a stable version of jar we have used. Add this jar to your application build path/class path/libraries. Add log4j.properties file under your src folder (empty package). log4j.properties will contain following lines:

log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = com.logging.NewLogForEachRunFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{dd-MM-yyyy} %d{ABSOLUTE} %5p %c:%L - %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = E:/Logs/MyApplication/logs.log

您可以根据环境而不是DEBUG更改日志级别(生产应使用INFO或ERROR). 然后在带有以下代码的 com.logging 包中添加一个类 NewLogForEachRunFileAppender .

You can change the log level as per environment instead of DEBUG(Production should use INFO or ERROR). Then add a class NewLogForEachRunFileAppender in com.logging package with following code.

package com.logging;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.ErrorCode;

/**
 * This is a customized log4j appender, which will create a new line for every
 * run of the application.
 */
public class NewLogForEachRunFileAppender extends FileAppender {

    public NewLogForEachRunFileAppender() {
    }

    public NewLogForEachRunFileAppender(Layout layout, String filename,
            boolean append, boolean bufferedIO, int bufferSize)
            throws IOException {
        super(layout, filename, append, bufferedIO, bufferSize);
    }

    public NewLogForEachRunFileAppender(Layout layout, String filename,
            boolean append) throws IOException {
        super(layout, filename, append);
    }

    public NewLogForEachRunFileAppender(Layout layout, String filename)
            throws IOException {
        super(layout, filename);
    }

    public void activateOptions() {
        if (fileName != null) {
            try {
                fileName = getNewLogFileName();
                setFile(fileName, fileAppend, bufferedIO, bufferSize);
            } catch (Exception e) {
                errorHandler.error("Error while activating log options", e,
                        ErrorCode.FILE_OPEN_FAILURE);
            }
        }
    }

    public String getNewLogFileName() {
        if (fileName != null) {
            final String DOT = ".";
            final String HIPHEN = "-";
            final File logFile = new File(fileName);
            final String fileName = logFile.getName();
            String newFileName = "";

            final int dotIndex = fileName.indexOf(DOT);
            if (dotIndex != -1) {
                // the file name has an extension. so, insert the time stamp
                // between the file name and the extension
                newFileName = fileName.substring(0, dotIndex) + HIPHEN
                        + getDate(System.currentTimeMillis(), "yyyyMMdd") + DOT
                        + fileName.substring(dotIndex + 1);
            } else {
                // the file name has no extension. So, just append the timestamp
                // at the end.
                newFileName = fileName + HIPHEN
                        + getDate(System.currentTimeMillis(), "yyyyMMdd");
            }
            return logFile.getParent() + File.separator + newFileName;
        }
        return null;
    }

    public static String getDate(long milliSeconds, String dateFormat) {
        // Create a DateFormatter object for displaying date in specified
        // format.
        DateFormat formatter = new SimpleDateFormat(dateFormat);

        // Create a calendar object that will convert the date and time value in
        // milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);
        return formatter.format(calendar.getTime());
    }
}

然后您要在其中进行日志记录的类(例如MyClass)应包含以下代码:

Then your class (e.g., MyClass) in which you want to do logging should contain code like this:

   package com.hpcl.cel;

import org.apache.log4j.Logger;

public class MyClass {
    private static final Logger logger = Logger.getLogger(MyClass.class);

    public void showLogging() {
        logger.info("Operation started");

        logger.info("Operation finished");
    } 
}

现在构建并运行您的应用程序.调用MyClass的showLogging()方法后,它将创建一个名为

Now build and run your application. Once you will call the showLogging() method of MyClass, it will create a log file with name

logs-20160107.txt

logs-20160107.txt

在路径

E:/Logs/MyApplication

E:/Logs/MyApplication

示例日志将为:

07-01-2016 08:58:53,189 DEBUG Operation Started 
07-01-2016 08:58:53,190 DEBUG Operation Finished

这篇关于WebSphere如何将计时跟踪记录到单独的日志中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆