-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qiulinmin
committed
Nov 28, 2017
1 parent
ec780c4
commit cd277d0
Showing
5 changed files
with
80 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package me.pqpo.log4a; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
import me.pqpo.librarylog4a.Level; | ||
import me.pqpo.librarylog4a.appender.AbsAppender; | ||
|
||
/** | ||
* Created by pqpo on 2017/11/28. | ||
*/ | ||
public class NoBufferFileAppender extends AbsAppender { | ||
|
||
private File logFile; | ||
private OutputStream outputStream; | ||
|
||
public NoBufferFileAppender(File logFile) { | ||
this.logFile = logFile; | ||
openFileOutputStream(); | ||
} | ||
|
||
private void openFileOutputStream() { | ||
if (!logFile.exists()) { | ||
try { | ||
if(!logFile.createNewFile()) { | ||
return; | ||
} | ||
outputStream = new FileOutputStream(logFile); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void doAppend(int logLevel, String tag, String msg) { | ||
if (outputStream == null) { | ||
return; | ||
} | ||
String logStr = String.format("%s/%s: %s\n", Level.getShortLevelName(logLevel), tag, msg); | ||
try { | ||
outputStream.write(logStr.getBytes()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void release() { | ||
super.release(); | ||
if (outputStream != null) { | ||
try { | ||
outputStream.flush(); | ||
outputStream.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters