Search in sources :

Example 1 with LogConfigBean

use of org.eweb4j.config.bean.LogConfigBean in project eweb4j-framework by laiweiwei.

the class LogFactory method getLogger.

public static Log getLogger(Class<?> clazz, boolean isConsole, String level, String file, String fileSize) {
    LogConfigBean log = new LogConfigBean();
    log.setLevel(level);
    log.setFile(file);
    log.setSize("" + CommonUtil.parseFileSize(fileSize));
    log.setConsole(String.valueOf(isConsole));
    LogsConfigBean logs = new LogsConfigBean();
    logs.getLog().add(log);
    return new LogImpl(logs, "LOG", clazz);
}
Also used : LogsConfigBean(org.eweb4j.config.bean.LogsConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean)

Example 2 with LogConfigBean

use of org.eweb4j.config.bean.LogConfigBean in project eweb4j-framework by laiweiwei.

the class LogFactory method getLogger.

public static Log getLogger(Class<?> clazz, boolean isConsole) {
    LogConfigBean log = new LogConfigBean();
    log.setLevel("debug");
    log.setFile(null);
    log.setSize("0");
    log.setConsole(String.valueOf(isConsole));
    LogsConfigBean logs = new LogsConfigBean();
    logs.getLog().add(log);
    return new LogImpl(logs, "CONFIG", clazz);
}
Also used : LogsConfigBean(org.eweb4j.config.bean.LogsConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean)

Example 3 with LogConfigBean

use of org.eweb4j.config.bean.LogConfigBean in project eweb4j-framework by laiweiwei.

the class LogImpl method write.

private synchronized String write(final int level, final String msg, Throwable th) {
    String mess = msg;
    if (th != null) {
        StringWriter strWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(strWriter, true);
        th.printStackTrace(writer);
        mess = new StringBuilder(mess).append(" cause by:\n\t" + strWriter.getBuffer().toString()).toString();
    }
    if (logs == null || logs.getLog() == null)
        return mess;
    StringBuilder result = new StringBuilder();
    for (LogConfigBean log : logs.getLog()) {
        /* 如果当前给定的日志级别小于配置的允许级别,不予记录 */
        if (LogLevel.level(log.getLevel()) == 0 || level < LogLevel.level(log.getLevel())) {
            return mess;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append(LogLevel.level(level).toUpperCase());
        sb.append("] ");
        sb.append(CommonUtil.getNowTime("yyyy-MM-dd HH:mm:ss"));
        sb.append(this.module.toLowerCase());
        sb.append(" ~ ");
        StackTraceElement s = new Exception(this.clazz.getName()).getStackTrace()[2];
        String m = s.getClassName() + "." + s.getMethodName();
        sb.append(s.toString().replace(m, ""));
        sb.append(" ");
        sb.append(mess);
        result.append(sb.toString());
        if ("1".equals(log.getConsole()) || "true".equalsIgnoreCase(log.getConsole())) {
            if (level > 3)
                System.err.println(sb.toString());
            else
                System.out.println(sb.toString());
        }
        BufferedWriter bw = null;
        try {
            if (log.getFile() != null && log.getFile().trim().length() > 0) {
                File file = new File(ConfigConstant.CONFIG_BASE_PATH + log.getFile());
                if (!file.exists()) {
                    File parentDir = file.getParentFile();
                    if (!parentDir.isDirectory() || !parentDir.exists())
                        return result.toString();
                    FileUtil.createFile(file.getAbsolutePath());
                    System.out.println("log file->" + file.getAbsolutePath() + " create...");
                }
                if (file.length() / (1024 * 1024) >= Integer.parseInt(log.getSize())) {
                    File tf = new File(file.getAbsolutePath() + "." + CommonUtil.getNowTime("_MMddHHmmss"));
                    FileUtil.copy(file, tf);
                    file.delete();
                    file = null;
                    file = new File(log.getFile());
                }
                bw = new BufferedWriter(new FileWriter(file, true));
                bw.newLine();
                bw.write(sb.toString());
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.flush();
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return result.toString();
}
Also used : StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) File(java.io.File) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

LogConfigBean (org.eweb4j.config.bean.LogConfigBean)3 LogsConfigBean (org.eweb4j.config.bean.LogsConfigBean)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1