OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012-2015 DataStax Inc. | 2 * Copyright (C) 2012-2015 DataStax Inc. |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
5 * you may not use this file except in compliance with the License. | 5 * you may not use this file except in compliance with the License. |
6 * You may obtain a copy of the License at | 6 * You may obtain a copy of the License at |
7 * | 7 * |
8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
9 * | 9 * |
10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
(...skipping 12 matching lines...) Expand all Loading... |
23 to utilize this API. | 23 to utilize this API. |
24 For performance purposes disk flushing should be disabled. Alternatively disk io
can be | 24 For performance purposes disk flushing should be disabled. Alternatively disk io
can be |
25 disabled too. | 25 disabled too. |
26 */ | 26 */ |
27 public class FileLogger { | 27 public class FileLogger { |
28 | 28 |
29 private boolean do_file_logging = true; | 29 private boolean do_file_logging = true; |
30 private boolean do_console_logging = false; | 30 private boolean do_console_logging = false; |
31 private boolean do_disk_flushing = true; | 31 private boolean do_disk_flushing = true; |
32 private String log_file_name; | 32 private String log_file_name; |
33 private FileWriter file_log = null; | 33 BufferedWriter file_log = null; |
| 34 private final Integer file_buffer_size_bytes = 16384; |
34 | 35 |
35 public FileLogger( | 36 public FileLogger( |
36 String log_file_name, boolean do_file_logging, | 37 String log_file_name, boolean do_file_logging, |
37 boolean do_console_logging, boolean do_disk_flushing) { | 38 boolean do_console_logging, boolean do_disk_flushing) { |
38 init(log_file_name, do_file_logging, do_console_logging, do_disk_flushin
g); | 39 init(log_file_name, do_file_logging, do_console_logging, do_disk_flushin
g); |
39 } | 40 } |
40 | 41 |
41 public FileLogger( | 42 public FileLogger( |
42 String log_file_name, boolean do_file_logging, boolean do_console_loggin
g) { | 43 String log_file_name, boolean do_file_logging, boolean do_console_loggin
g) { |
43 init(log_file_name, do_file_logging, do_console_logging, true); | 44 init(log_file_name, do_file_logging, do_console_logging, true); |
44 } | 45 } |
45 | 46 |
46 private void init( | 47 private void init( |
47 String log_file_name, boolean do_file_logging, | 48 String log_file_name, boolean do_file_logging, |
48 boolean do_console_logging, boolean do_disk_flushing) { | 49 boolean do_console_logging, boolean do_disk_flushing) { |
49 | 50 |
50 this.log_file_name = log_file_name; | 51 this.log_file_name = log_file_name; |
51 this.do_file_logging = do_file_logging; | 52 this.do_file_logging = do_file_logging; |
52 this.do_console_logging = do_console_logging; | 53 this.do_console_logging = do_console_logging; |
53 this.do_disk_flushing = do_disk_flushing; | 54 this.do_disk_flushing = do_disk_flushing; |
54 | 55 |
55 if (do_file_logging) { | 56 if (do_file_logging) { |
56 try { | 57 try { |
57 file_log = new FileWriter(log_file_name); | 58 FileWriter ofile = new FileWriter(log_file_name); |
| 59 file_log = new BufferedWriter(ofile, file_buffer_size_bytes); |
| 60 |
58 } catch (IOException e) { | 61 } catch (IOException e) { |
59 System.err.println("IOException: "+ log_file_name + " " + e); | 62 System.err.println("IOException: "+ log_file_name + " " + e); |
60 } | 63 } |
61 } | 64 } |
62 } | 65 } |
63 | 66 |
64 public void filelog(String log_msg){ | 67 public void filelog(String log_msg){ |
65 if (do_file_logging && file_log != null) { | 68 if (do_file_logging && file_log != null) { |
66 try { | 69 try { |
67 file_log.write(System.currentTimeMillis()+" " +log_msg+"\n"); | 70 file_log.write(System.currentTimeMillis()+" " +log_msg+"\n"); |
68 if (do_disk_flushing) { | 71 if (do_disk_flushing) { |
69 file_log.flush(); | 72 file_log.flush(); |
70 } | 73 } |
71 | 74 |
72 } catch (IOException e) { | 75 } catch (IOException e) { |
73 System.err.println("writeIOException: " + log_file_name + " " +
e); | 76 System.err.println("writeIOException: " + log_file_name + " " +
e); |
74 } | 77 } |
75 } | 78 } |
76 | 79 |
77 if (do_console_logging) { | 80 if (do_console_logging) { |
78 System.err.println(log_msg); | 81 System.err.println(log_msg); |
79 } | 82 } |
80 } | 83 } |
81 } | 84 } |
OLD | NEW |