服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|

服务器之家 - 编程语言 - JAVA教程 - java异步写日志到文件中实现代码

java异步写日志到文件中实现代码

2020-09-17 14:58nature_fly088 JAVA教程

这篇文章主要介绍了java异步写日志到文件中实现代码的相关资料,需要的朋友可以参考下

java异步日志文件中详解

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.tydic.ESUtil;
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
 
public class LogWriter {
// 日志的配置文件
  public static final String LOG_CONFIGFILE_NAME = "log.properties";
  // 日志文件名在配置文件中的标签
  public static final String LOGFILE_TAG_NAME = "logfile";
   
  // 默认的日志文件的路径和文件名称
  private final String DEFAULT_LOG_FILE_NAME = "./logtext.log";
  // 该类的唯一的实例
  private static LogWriter logWriter;
  // 文件输出流
  private PrintWriter writer; 
  // 日志文件名
  private String logFileName; 
  /**
   * 默认构造函数
   */
  private LogWriter() throws LogException{
    this.init();
  }
  private LogWriter(String fileName) throws LogException{
    this.logFileName = fileName;
    this.init();
  }
  /**
   * 获取LogWriter的唯一实例。
   * @return
   * @throws LogException
   */
  public synchronized static LogWriter getLogWriter()throws LogException{
    if (logWriter == null){
      logWriter = new LogWriter();
    }
    return logWriter;
  }
  public synchronized static LogWriter getLogWriter(String logFileName)throws LogException{
    if (logWriter == null){
      logWriter = new LogWriter(logFileName);
    }
    return logWriter;
  }
 
  /**
   * 往日志文件中写一条日志信息
   * 为了防止多线程同时操作(写)日志文件,造成文件”死锁”。使用synchronized关键字
   * @param logMsg  日志消息
   */
  public synchronized void log(String logMsg) {
    this.writer.println(new java.util.Date() + ": " + logMsg);
  }
  /**
   * 往日志文件中写一条异常信息
   * 使用synchronized关键字。
   * @param ex  待写入的异常
   */
  public synchronized void log(Exception ex) {
    writer.println(new java.util.Date() + ": ");
    ex.printStackTrace(writer);
  }
 
  /**
   * 初始化LogWriter
   * @throws LogException
   */
  private void init() throws LogException{
    //如果用户没有在参数中指定日志文件名,则从配置文件中获取。
    if (this.logFileName == null){
      this.logFileName = this.getLogFileNameFromConfigFile();
      //如果配置文件不存在或者也没有指定日志文件名,则用默认的日志文件名。
      if (this.logFileName == null){
        this.logFileName = DEFAULT_LOG_FILE_NAME;
      }
    }
    File logFile = new File(this.logFileName);
    try {
      // 其中的FileWriter()中的第二个参数的含义是:是否在文件中追加内容
      // PrintWriter()中的第二个参数的含义是:自动将数据flush到文件中
      writer = new PrintWriter(new FileWriter(logFile, true), true);
      System.out.println("日志文件的位置:" + logFile.getAbsolutePath());
    } catch (IOException ex) {
      String errmsg = "无法打开日志文件:" + logFile.getAbsolutePath();
      // System.out.println(errmsg);
      throw new LogException(errmsg, ex);
    }
  }
  /**
   * 从配置文件中取日志文件名
   * @return
   */
  private String getLogFileNameFromConfigFile() { 
    try
      Properties pro = new Properties(); 
      //在类的当前位置,查找属性配置文件log.properties 
      InputStream fin = getClass().getResourceAsStream(LOG_CONFIGFILE_NAME); 
      if (fin != null){
        pro.load(fin);//载入配置文件
        fin.close(); 
        return pro.getProperty(LOGFILE_TAG_NAME);
      } else {
        System.err.println("无法打开属性配置文件: log.properties" ); 
      }
    }catch (IOException ex) { 
      System.err.println("无法打开属性配置文件: log.properties" ); 
    }
    return null;
  }
  //关闭LogWriter
  public void close() {
    logWriter = null;
    if (writer != null){
      writer.close();
    }
  }
 
  public static void main(String[] args) {
    LogWriter logger = null;
    try {
      String fileName = "d:/temp/logger.log";
      logger = LogWriter.getLogWriter(fileName);
//     logger.log("First log!");
//     logger.log("第二个日志信息");
//     logger.log("Third log");
//     logger.log("第四个日志信息");
      String content="tableaA|device_number|13701010";
      StringBuffer sb=new StringBuffer();
      for(int i=0;i<1000000;i++){
        sb.append(content).append(i).append(";\n");
      }
      content=sb.toString();
      long startTime=System.currentTimeMillis();
      logger.log(content);
      long endTime=System.currentTimeMillis();
      System.out.println("总消耗时间:"+(endTime-startTime));
      logger.close();
//     ReadFromFile.readFileByLines(fileName);
    } catch (LogException e) {
      e.printStackTrace();
    }
  }
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.tydic.ESUtil;
 
public class AychWriter extends Thread {
  private String content;
  public AychWriter(String content){
    this.content=content;
  }
  @Override
  public void run(){
    System.out.println("开始执行run()");
    LogWriter logger = null;
    String fileName = "d:/temp/logger.log";
    long startTime=System.currentTimeMillis();
    try {
      logger = LogWriter.getLogWriter(fileName);
      logger.log(this.content);
    } catch (LogException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
 
    long endTime=System.currentTimeMillis();
    System.out.println("总消耗时间:"+(endTime-startTime));
  }
}

测试类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.tydic.ESUtil;
 
import java.io.FileWriter;
import java.io.IOException;
 
import org.junit.Test;
 
public class test_test {
  /**
   * 同步向指定文件尾部写入字符串
   */
public void testAppendMethodB(String fileName,String content) throws IOException{
    try {
      //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
      FileWriter writer = new FileWriter(fileName, true);
      writer.write(content);
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    }
 
/**
 *调用上面同步写方法
 */
  @Test
  public void testWriteTOFile() throws IOException{
    String fileName = "d:\\test.txt";
    String content="tableaA|device_number|13701010";
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<100000;i++){
      sb.append(content).append(i).append(";\n");
    }
    content=sb.toString();
    long startTime=System.currentTimeMillis();
    testAppendMethodB(fileName,content);
    long endTime=System.currentTimeMillis();
    System.out.println("总消耗时间:"+(endTime-startTime));
  }
  /**
   * 异步调用写方法
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testAsyncWriteTOFile() throws IOException, InterruptedException{
    String fileName = "d:\\test.txt";
    String content="tableaA|device_number|13701010";
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<100000;i++){
      sb.append(content).append(i).append(";\n");
    }
    content=sb.toString();
    System.out.println("start write...");
    new AychWriter(content).start();
    System.out.println("write over...");
    Thread.sleep(30000); //重要,如果主线程挂了,调用线程也停止了
    System.out.println("main Thread over");
  }
   
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/nature_fly088/article/details/54584802

延伸 · 阅读

精彩推荐