Java 记录 日志,log,



public class LogUtil
{

    private static Logger  log               = Logger.getLogger(LogUtil.class.getName());
    private static Handler fileHandler       = null;

    static
    {
        try
        {
            String logfile = (new File(ServiceAccess.getSystemSupportService().getDeployedPar("an-commonsh-ftpcsv-emf").getBaseDir()))
                            .getAbsolutePath() + File.separator + "log" + File.separator + "ftpcsv%g.log";
            // String dirPath = CommonUtil.getDeployedParBaseDir("zxnm01-northbound");
            // String logfile = (new File((new File(dirPath)).getParent())).getParent() + File.separator + "log" + File.separator + "tl1" +
            // File.separator +"tl1agent_AlarmTrace%g.log";
            fileHandler = new FileHandler(logfile, 5000000, 5, true);
            // fileHandler = new FileHandler(CommonUtil.getNetNumenHomeDir() + "/ums-svr/log/tl1agent_AlarmTrace%g.log" ,10000,11,true);
            fileHandler.setLevel(Level.INFO);
            fileHandler.setFormatter(new CustomFormatter());

        }
        catch(Exception ex)
        {
        }
        log.addHandler(fileHandler);
    }

    public static void info(String loginfo)
    {
        log.info(loginfo);
    }

}











public class CustomFormatter extends Formatter
{
    // 时间
    private Date          dat           = new Date();
    // 参数
    private Object[]      args          = new Object[1];
    // 消息格式化器
    private MessageFormat formatter     = null;
    // 时间参数
    private String        format        = "{0,date} {0,time}";
    // 行分格符
    private String        lineSeparator = (String)AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));

    public String format(LogRecord record)
    {
        StringBuffer sb = new StringBuffer();
        dat.setTime(record.getMillis());
        args[0] = dat;
        StringBuffer text = new StringBuffer();
        if(formatter == null)
        {
            formatter = new MessageFormat(format);
        }
        formatter.format(args, text, null);
        sb.append(text);
        sb.append("  ");
        String message = formatMessage(record);
        sb.append(message);
        sb.append(lineSeparator);
        printWriterLog(record, sb);
        return sb.toString();
    }

    private void printWriterLog(LogRecord record, StringBuffer sb)
    {
        if(record.getThrown() != null)
        {
            try
            {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                record.getThrown().printStackTrace(pw);
                pw.close();
                sb.append(sw.toString());
            }
            catch(Exception ex)
            {
                 
            }
        }
    }
}



















你可能感兴趣的:(java)