自定义日志类

package com.customcode.util;

import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.TimeUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  日志生成位置在ecology\log\actionlog
 * @Author:
 * @Date:
 * 
 **/
public class XmActionLog {
    public BaseBean baseBean = new BaseBean();
    private static char systemSeparator = File.separatorChar;
    private static String logoPath = GCONST.getRootPath() + "log"+systemSeparator+"actionlog";

    /**
     *
     * @param flag
     * @return
     */
    private boolean init(String flag) {
        boolean state = false;
        String flagVal = baseBean.getPropValue("XmActionLog", flag);
        if ("true".equalsIgnoreCase(flagVal)) {
            state = true;
        }
        return state;
    }

    public String debug(String actionName, String Msg) {
        return writeLog("debug", actionName, Msg);
    }

    public String info(String actionName, String Msg) {
        return writeLog("info", actionName, Msg);
    }

    public String warn(String actionName, String Msg) {
        return writeLog("warn", actionName, Msg);
    }

    public String error(String actionName, String Msg) {
        return writeLog("error", actionName, Msg);
    }

    /**
     * 生成日志
     *
     * @param flag
     * @param actionName
     * @param message
     * @return
     */
    private String writeLog(String flag, String actionName, String message) {
        boolean status = init(flag);
        if (status) {
            String currentDate = TimeUtil.getCurrentDateString();
            String currentTime = TimeUtil.getOnlyCurrentTimeString();
            message = currentTime + "  "+flag+"===>>" + message;
            if(flag.length()>4) {
            	message = currentTime + "  "+flag+"==>>" + message;
            }
            File dir = new File(logoPath + systemSeparator + actionName);
            if (!dir.exists()) dir.mkdirs();
            File fileName = new File(logoPath + systemSeparator + actionName + systemSeparator + actionName+"_"+flag+"_"+currentDate + "_log.txt");
            if (!fileName.exists()) {
                try {
                    fileName.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileWriter writer = null;
            try {
                writer = new FileWriter(fileName, true);
                writer.write(message + "\r\n");
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    if (writer != null)
                        writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } finally {
                try {
                    if (writer != null)
                        writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
	public String outlog(String actionName, String meg) {
		return outlog("debug", actionName,  meg);
	}
	
	public String outlog(String flag, String actionName, String meg) {
		boolean status = init(flag);	
		if(status) {
			SimpleDateFormat CurrentDate = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat CurrentTime = new SimpleDateFormat("HH:mm:ss");
			Date date = new Date();
			String thisDate = CurrentDate.format(date);
			String thisTime = CurrentTime.format(date);
			meg = "==>>" + thisTime + "==>>" + meg;
			File dir = new File(logoPath);
			if (!dir.exists()) dir.mkdir();
			File fileName = new File(logoPath+"/"+thisDate+"_"+actionName+"_log.txt");
			if (!fileName.exists()) {
				try {
					fileName.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			FileWriter writer = null;
			try {
				writer = new FileWriter(fileName, true);
				writer.write(meg + "\r\n");
			} catch (IOException e) {
				e.printStackTrace();
				try {
					if (writer != null)
						writer.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			} finally {
				try {
					if (writer != null)
						writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	  return "0";
	}
	
	
	/**
	 * 输出日志到某个文件夹下
	 * @param meg   日志信息
	 * @param Directory  存放文件夹名称    (默认在ecology/log/下)
	 * @param type  日志类型  
	 */
	public  void outTolog( String meg,String Directory,String type){
		SimpleDateFormat format_01= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String str = format_01.format(date);
		meg=str+"      "+meg;
		File directory = new File(GCONST.getRootPath() +"/log"+ File.separator + Directory);//
		if (!directory.exists()) {//目录不存在就创建
			directory.mkdir();
		}
		SimpleDateFormat format_02= new SimpleDateFormat("yyyy-MM-dd");
		String strDate=format_02.format(date);
		File fileName = new File ("");
		fileName = new File (directory+"/Record_log_"+strDate+".txt");//
		
		if(!fileName.exists()){//文件不存在就创建
			try {
				fileName.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		FileWriter writer = null;  
		try {         
			writer = new FileWriter(fileName, true);     
			writer.write(meg+"\r\n");       
		} catch (IOException e) {     
			e.printStackTrace();     
		} finally {     
			try {     
				if(writer != null){  
					writer.close();     
				}  
			} catch (IOException e) {     
				e.printStackTrace();     
			}     
		}
		
		
	}
}

放在classbean文件下的自定义文件夹重启编译,然后再weaver/ecology/WEB-INF/prop路径下放入XmActionLog.properties配置文件,具体配置如下:

#Debug
debug=true

#Info
info=true

#Warn
warn=true

#Error
error=true

#Fatal
fatal=true

做完这些就可以,在平时的接口中引用日志类。日志的产生路径为:weaver/ecology/log/actionlog

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值