Logger 具体到方法的日志输出类

187 篇文章 0 订阅
68 篇文章 0 订阅
package com.segsec.gisap;

import java.io.*;
import java.util.Properties;
import java.text.SimpleDateFormat;

public class Logger {
    private static Logger logger;
    private static String logPath;
    private static String eventPath;
    private static String website;
    private FileWriter fwlogger =null;
    private StackTraceElement elements[];
    private static String ClassMethodName="";
    
    protected Logger() {
        InputStream is = null;
        Properties properties = new Properties();
        try {
        	is = getClass().getResourceAsStream("/logger.properties");
        	properties.load(is);
	        logPath = properties.getProperty("logPath", "C:\\");
	        website = properties.getProperty("website", "");
        } catch (IOException ex) {
            System.err.println("不能读取属性文件.请确保log.properties在CLASSPATH指定的路径中");
        }finally{
        	try {
        		if(is!=null){
        			is.close();
        		}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    }

    public static Logger getLogger() {
        try {
            if (logger == null) {
                logger = new Logger();
            }
        } catch (Exception e) {
            System.out.println("创建日志文件失败:" + e.toString());
            e.printStackTrace();
        }
        return logger;
    }

    //获得日志文件目录的上一级目录
    public String getLogParentPath() {
        int ss = logPath.length();
        String strPath = logPath.substring(0, ss - 1);
        ss = strPath.lastIndexOf('\\');
        String retPath = strPath.substring(0, ss + 1);
        return retPath;
    }

    public void setLogPath(String path) {
        if (path == null) {
            logPath = "";
        } else {
            logPath = path;
        }
    }

    public synchronized void writeLog(String msg) {
        StringBuffer fileName = new StringBuffer();

        if (logPath != null) {
            fileName.append(logPath);
        } else {
            System.out.println("logpath is null!");
        }
        elements = new Throwable().getStackTrace();
        ClassMethodName=elements[1].getClassName() + "." + elements[1].getMethodName();
        if("".equalsIgnoreCase(website)){
            fileName.append("Log_");
        }else{
            fileName.append("Log_" + website + "_" +ClassMethodName+"_");
        }

        java.sql.Timestamp nowDate = new java.sql.Timestamp(System.currentTimeMillis());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        String aa = sdf.format(nowDate);
        fileName.append(aa);
        fileName.append(".txt");
        try {
            fwlogger = new FileWriter(fileName.toString(), true);
            StringBuffer sbMsg = new StringBuffer();
            sbMsg.append(getLongTimeString() + ":\r\n");
            sbMsg.append(msg);
            sbMsg.append("\r\n");
            fwlogger.write(sbMsg.toString());
        } catch (Exception e) {
            System.out.println("writer log error:" + e.toString());
        }finally{
        	try {
        		if(fwlogger!=null){
                    fwlogger.close();
        		}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    }


    public synchronized void writeErrorLog(String msg) {
        StringBuffer fileName = new StringBuffer();

        if (logPath != null) {
            fileName.append(logPath);
        } else {
            System.out.println("logpath is null!");
        }

        elements = new Throwable().getStackTrace();
        ClassMethodName=elements[1].getClassName() + "." + elements[1].getMethodName();
        if("".equalsIgnoreCase(website)){
            fileName.append("ErrorLog_");
        }else{
            fileName.append("ErrorLog_" + website + "_" +ClassMethodName+"_");
        }

        java.sql.Timestamp nowDate = new java.sql.Timestamp(System.currentTimeMillis());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        
        
        String aa = sdf.format(nowDate);
        fileName.append(aa);
        fileName.append(".txt");
        try {
            fwlogger = new FileWriter(fileName.toString(), true);
            StringBuffer sbMsg = new StringBuffer();
            sbMsg.append(getLongTimeString() + ":\r\n");
            sbMsg.append("Error:"+msg);
            sbMsg.append("\r\n");
            fwlogger.write(sbMsg.toString());
        } catch (Exception e) {
            System.out.println("writer log error:" + e.toString());
        }finally{
        	try {
        		if(fwlogger!=null){
                    fwlogger.close();
        		}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    }

    public synchronized void writeDebugLog(String msg) {
        StringBuffer fileName = new StringBuffer();

        if (logPath != null) {
            fileName.append(logPath);
        } else {
            System.out.println("logpath is null!");
        }

        elements = new Throwable().getStackTrace();
        ClassMethodName=elements[1].getClassName() + "." + elements[1].getMethodName();
        if("".equalsIgnoreCase(website)){
            fileName.append("DebugLog_");
        }else{
            fileName.append("DebugLog_" + website + "_" +ClassMethodName+"_");
        }

        java.sql.Timestamp nowDate = new java.sql.Timestamp(System.currentTimeMillis());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        
        
        String aa = sdf.format(nowDate);
        fileName.append(aa);
        fileName.append(".txt");
        try {
            fwlogger = new FileWriter(fileName.toString(), true);
            StringBuffer sbMsg = new StringBuffer();
            sbMsg.append(getLongTimeString() + ":\r\n");
            sbMsg.append("Debug:"+msg);
            sbMsg.append("\r\n");
            fwlogger.write(sbMsg.toString());
        } catch (Exception e) {
            System.out.println("writer log error:" + e.toString());
        }finally{
        	try {
        		if(fwlogger!=null){
                    fwlogger.close();
        		}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    }
    
    public synchronized void writeDebugInfo(String msg) {
        StringBuffer fileName = new StringBuffer();

        if (logPath != null) {
            fileName.append(logPath);
        } else {
            System.out.println("logpath is null!");
        }

        elements = new Throwable().getStackTrace();
        ClassMethodName=elements[1].getClassName() + "." + elements[1].getMethodName();
        if("".equalsIgnoreCase(website)){
            fileName.append("DebugInfo_");
        }else{
            fileName.append("DebugInfo_" + website + "_" +ClassMethodName+"_");
        }

        java.sql.Timestamp nowDate = new java.sql.Timestamp(System.currentTimeMillis());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        
        
        String aa = sdf.format(nowDate);
        fileName.append(aa);
        fileName.append(".txt");
        try {
            fwlogger = new FileWriter(fileName.toString(), true);
            StringBuffer sbMsg = new StringBuffer();
            sbMsg.append(getLongTimeString() + ":\r\n");
            sbMsg.append("Debug:"+msg);
            sbMsg.append("\r\n");
            fwlogger.write(sbMsg.toString());
        } catch (Exception e) {
            System.out.println("writer log error:" + e.toString());
        }finally{
        	try {
        		if(fwlogger!=null){
                    fwlogger.close();
        		}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    }

    //系统日志
    public String getEventParentPath() {
        int ss = eventPath.length();
        String strPath = eventPath.substring(0, ss - 1);
        ss = strPath.lastIndexOf('\\');
        String retPath = strPath.substring(0, ss + 1);
        return retPath;
    }

    public void setEventPath(String path) {
        if (path == null) {
            eventPath = "";
        } else {
            eventPath = path;
        }
    }

    public synchronized void writeEvent(String msg) {
        StringBuffer fileName = new StringBuffer();
        if (eventPath != null) {
            fileName.append(eventPath);
        } else {
            System.out.println("Eventpath is null ");
        }

        elements = new Throwable().getStackTrace();
        ClassMethodName=elements[1].getClassName() + "." + elements[1].getMethodName();
        if("".equalsIgnoreCase(website)){
            fileName.append("System_Event_");
        }else{
            fileName.append("System_Event_" + website + "_" +ClassMethodName+"_");
        }
        java.sql.Timestamp nowDate = new java.sql.Timestamp(System.currentTimeMillis());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        String aa = sdf.format(nowDate);
        fileName.append(aa);
        fileName.append(".txt");
        try {
        	fwlogger = new FileWriter(fileName.toString(), true);
            StringBuffer sbMsg = new StringBuffer();
            sbMsg.append(getLongTimeString() + ":\r\n");
            sbMsg.append(msg);
            sbMsg.append("\r\n");
            fwlogger.write(sbMsg.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	try {
        		if(fwlogger!=null){
                    fwlogger.close();
        		}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    }
    /**
     * writeDebugEvent
     * 写专有日志标记,主要用于数据刷新较快的情况下处理专门的日志便于查看
     * @param eventFlag 日志事件标签,给一个易于标识和查询的标识,关键易于查找
     * @param msg 要写入的消息内容
     */
    public synchronized void writeDebugEvent(String eventFlag,String msg) {
        StringBuffer fileName = new StringBuffer();
        if (eventPath != null) {
            fileName.append(eventPath);
        } else {
            System.out.println("Eventpath is null ");
        }

        elements = new Throwable().getStackTrace();
        ClassMethodName=elements[1].getClassName() + "." + elements[1].getMethodName();
        if("".equalsIgnoreCase(website)){
            fileName.append(eventFlag+"_");
        }else{
            fileName.append(eventFlag+"_" + website + "_" +ClassMethodName+"_");
        }
        java.sql.Timestamp nowDate = new java.sql.Timestamp(System.currentTimeMillis());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        String aa = sdf.format(nowDate);
        fileName.append(aa);
        fileName.append(".txt");
        try {
        	fwlogger = new FileWriter(fileName.toString(), true);
            StringBuffer sbMsg = new StringBuffer();
            sbMsg.append(getLongTimeString() + ":\r\n");
            sbMsg.append(msg);
            sbMsg.append("\r\n");
            fwlogger.write(sbMsg.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	try {
        		if(fwlogger!=null){
                    fwlogger.close();
        		}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    }

    public static String getEncodeString(String s) {
        String retStr = "";
        try {
            if (s != null) {
                retStr = new String(s.getBytes("ISO-8859-1"), "GBK");
            }
        } catch (Exception ex) {
            System.out.println(s + " getEncodeString error:" + ex.toString());
        }
        return retStr;
    }

    //生成长时间字符串,形如:2005-01-01 01:01:01
    public static String getLongTimeString() {
        java.util.Date myDate = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //long myTime = (myDate.getTime() / 1000) - 60 * 60 * 24 * 365;
        //myDate.setTime(myTime * 1000);
        String mDate = formatter.format(myDate);
        return mDate;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值