Blame view

src/test/java/com/essa/framework/Logger.java 2.52 KB
fdd4bb76   zengjin55   windows
1
2
3
4
5
6
7
8
9
10
  package com.essa.framework;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.OutputStreamWriter;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import com.essa.framework.LogType;
  
fdd4bb76   zengjin55   windows
11
12
  public class Logger {  
      
09fdf50f   Administrator   bpms
13
      public static String OutputFileName = getDateTimeByFormat(new Date(), "yyyyMMdd");  
fdd4bb76   zengjin55   windows
14
15
16
      private static OutputStreamWriter outputStreamWriter;  
      private static String logFileName;  
      public static boolean LogFlag = true;  
09fdf50f   Administrator   bpms
17
      public static String logContent;
fdd4bb76   zengjin55   windows
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    
      public Logger() {  
    
      }  
    
      private static void WriteLog(String logEntry) {  
    
          try {  
                    
                      // 定义日志文件保存路径和日志文件名称  
                  logFileName = ".\\Log" + "\\" + OutputFileName + ".log";  
                  if (outputStreamWriter == null) {  
                      File logFile = new File(logFileName);  
                    
                  if (!logFile.exists())  
                          logFile.createNewFile();  
                  //利用OutputStreamWriter往日志文件写内容,字符编码是unicode  
                  outputStreamWriter = new OutputStreamWriter(new FileOutputStream(logFileName), "utf-8");  
              }  
              outputStreamWriter.write(logEntry, 0, logEntry.length());  
09fdf50f   Administrator   bpms
38
              
fdd4bb76   zengjin55   windows
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
              outputStreamWriter.flush();  
    
          } catch (Exception e) {  
              System.out.println(LogType.LogTypeName.ERROR.toString() + ": Failed to write the file " + logFileName);  
              e.printStackTrace();  
    
          }  
    
      }  
    
      //获取当前系统时间,得到格式化时间字符串  
      private static String getDateTimeByFormat(Date date, String format) {  
    
          SimpleDateFormat df = new SimpleDateFormat(format);  
    
          return df.format(date);  
    
      }  
        
      public static void Output(LogType.LogTypeName logTypeName, String logMessage) {  
60b8852f   Administrator   test
59
      	Date date = new Date();  
fdd4bb76   zengjin55   windows
60
61
          String logTime = getDateTimeByFormat(date, "yyyy-MM-dd HH:mm:ss.SSS");  
          String logEntry = logTime + " " + logTypeName.name() + ": " + logMessage + "\r\n";  
09fdf50f   Administrator   bpms
62
63
          System.out.print(logEntry); 
          setLog(logEntry);
fdd4bb76   zengjin55   windows
64
65
66
67
          // 定义一个开关,为True就输出日志,如果你不想输出,改成False  
          if (LogFlag)  
              WriteLog(logEntry);  
          }  
09fdf50f   Administrator   bpms
68
69
70
71
72
73
74
75
76
77
78
      /**
       * 获取日志信息
       * @return
       */
      public static String getLog() {
      	return logContent;
      }
      
      public static void setLog(String logEntry) {
      	logContent = logEntry;
      }
fdd4bb76   zengjin55   windows
79
  }