Skip to content

Commit 0356d29

Browse files
committed
see 09/03 log
1 parent c4d71d6 commit 0356d29

File tree

2 files changed

+50
-83
lines changed

2 files changed

+50
-83
lines changed

app/src/main/java/com/blankj/androidutilcode/UtilsApp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void initLog() {
5959
.setLogHeadSwitch(true)// 设置log头信息开关,默认为开
6060
.setLog2FileSwitch(false)// 打印log时是否存到文件的开关,默认关
6161
.setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中
62+
.setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-MM-dd.txt"
6263
.setBorderSwitch(true)// 输出日志是否带边框开关,默认开
6364
.setConsoleFilter(LogUtils.V)// log的控制台过滤器,和logcat过滤器同理,默认Verbose
6465
.setFileFilter(LogUtils.V);// log文件过滤器,和logcat过滤器同理,默认Verbose

utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java

Lines changed: 49 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.json.JSONObject;
1010

1111
import java.io.BufferedWriter;
12-
import java.io.ByteArrayOutputStream;
1312
import java.io.File;
1413
import java.io.FileWriter;
1514
import java.io.IOException;
@@ -24,9 +23,6 @@
2423
import java.util.Locale;
2524
import java.util.concurrent.ExecutorService;
2625
import java.util.concurrent.Executors;
27-
import java.util.zip.DataFormatException;
28-
import java.util.zip.Deflater;
29-
import java.util.zip.Inflater;
3026

3127
import javax.xml.transform.OutputKeys;
3228
import javax.xml.transform.Source;
@@ -62,19 +58,21 @@ public final class LogUtils {
6258
private static final int FILE = 0x10;
6359
private static final int JSON = 0x20;
6460
private static final int XML = 0x30;
65-
private static ExecutorService executor;
66-
private static String defaultDir;// log默认存储目录
67-
private static String dir; // log存储目录
68-
69-
private static boolean sLogSwitch = true; // log总开关,默认开
70-
private static boolean sLog2ConsoleSwitch = true; // logcat是否打印,默认打印
71-
private static String sGlobalTag = null; // log标签
72-
private static boolean sTagIsSpace = true; // log标签是否为空白
73-
private static boolean sLogHeadSwitch = true; // log头部开关,默认开
74-
private static boolean sLog2FileSwitch = false;// log写入文件开关,默认关
75-
private static boolean sLogBorderSwitch = true; // log边框开关,默认开
76-
private static int sConsoleFilter = V; // log控制台过滤器
77-
private static int sFileFilter = V; // log文件过滤器
61+
62+
private static ExecutorService sExecutor;
63+
private static String sDefaultDir;// log默认存储目录
64+
private static String sDir; // log存储目录
65+
private static String sFilePrefix = "util";// log文件前缀
66+
private static boolean sLogSwitch = true; // log总开关,默认开
67+
private static boolean sLog2ConsoleSwitch = true; // logcat是否打印,默认打印
68+
private static String sGlobalTag = null; // log标签
69+
private static boolean sTagIsSpace = true; // log标签是否为空白
70+
private static boolean sLogHeadSwitch = true; // log头部开关,默认开
71+
private static boolean sLog2FileSwitch = false; // log写入文件开关,默认关
72+
private static boolean sLogBorderSwitch = true; // log边框开关,默认开
73+
private static int sConsoleFilter = V; // log控制台过滤器
74+
private static int sFileFilter = V; // log文件过滤器
75+
public static int sStackDeep = 1; // log栈深度
7876

7977
private static final Config CONFIG = new Config();
8078
private static final String FILE_SEP = System.getProperty("file.separator");
@@ -84,10 +82,9 @@ public final class LogUtils {
8482
private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════";
8583
private static final int MAX_LEN = 4000;
8684
private static final Format FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS ", Locale.getDefault());
87-
88-
private static final String NULL_TIPS = "Log with null object.";
89-
private static final String NULL = "null";
90-
private static final String ARGS = "args";
85+
private static final String NULL_TIPS = "Log with null object.";
86+
private static final String NULL = "null";
87+
private static final String ARGS = "args";
9188

9289
private LogUtils() {
9390
throw new UnsupportedOperationException("u can't instantiate me...");
@@ -214,9 +211,7 @@ private static String[] processTagAndHead(String tag) {
214211
StackTraceElement targetElement = new Throwable().getStackTrace()[3];
215212
String fileName = targetElement.getFileName();
216213
String className = fileName.substring(0, fileName.indexOf('.'));
217-
if (sTagIsSpace) {
218-
tag = isSpace(tag) ? className : tag;
219-
}
214+
if (sTagIsSpace) tag = isSpace(tag) ? className : tag;
220215
if (sLogHeadSwitch) {
221216
String head = new Formatter()
222217
.format("%s, %s(%s:%d)",
@@ -331,7 +326,7 @@ private static void print2File(final int type, final String tag, final String ms
331326
String format = FORMAT.format(now);
332327
String date = format.substring(0, 5);
333328
String time = format.substring(6);
334-
final String fullPath = (dir == null ? defaultDir : dir) + date + ".txt";
329+
final String fullPath = (sDir == null ? sDefaultDir : sDir) + sFilePrefix + "-" + date + ".txt";
335330
if (!createOrExistsFile(fullPath)) {
336331
Log.e(tag, "log to " + fullPath + " failed!");
337332
return;
@@ -344,10 +339,10 @@ private static void print2File(final int type, final String tag, final String ms
344339
.append(msg)
345340
.append(LINE_SEP);
346341
final String content = sb.toString();
347-
if (executor == null) {
348-
executor = Executors.newSingleThreadExecutor();
342+
if (sExecutor == null) {
343+
sExecutor = Executors.newSingleThreadExecutor();
349344
}
350-
executor.execute(new Runnable() {
345+
sExecutor.execute(new Runnable() {
351346
@Override
352347
public void run() {
353348
BufferedWriter bw = null;
@@ -397,113 +392,83 @@ private static boolean isSpace(final String s) {
397392
return true;
398393
}
399394

400-
public static byte[] compress(final byte input[]) {
401-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
402-
Deflater compressor = new Deflater(1);
403-
try {
404-
compressor.setInput(input);
405-
compressor.finish();
406-
final byte[] buf = new byte[2048];
407-
while (!compressor.finished()) {
408-
int count = compressor.deflate(buf);
409-
bos.write(buf, 0, count);
410-
}
411-
} finally {
412-
compressor.end();
413-
}
414-
return bos.toByteArray();
415-
}
416-
417-
public static byte[] uncompress(final byte[] input) {
418-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
419-
Inflater decompressor = new Inflater();
420-
try {
421-
decompressor.setInput(input);
422-
final byte[] buf = new byte[2048];
423-
while (!decompressor.finished()) {
424-
int count = 0;
425-
try {
426-
count = decompressor.inflate(buf);
427-
} catch (DataFormatException e) {
428-
e.printStackTrace();
429-
}
430-
bos.write(buf, 0, count);
431-
}
432-
} finally {
433-
decompressor.end();
434-
}
435-
return bos.toByteArray();
436-
}
437-
438-
439395
public static class Config {
440396
private Config() {
441-
if (defaultDir != null) return;
397+
if (sDefaultDir != null) return;
442398
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
443399
&& Utils.getApp().getExternalCacheDir() != null)
444-
defaultDir = Utils.getApp().getExternalCacheDir() + FILE_SEP + "log" + FILE_SEP;
400+
sDefaultDir = Utils.getApp().getExternalCacheDir() + FILE_SEP + "log" + FILE_SEP;
445401
else {
446-
defaultDir = Utils.getApp().getCacheDir() + FILE_SEP + "log" + FILE_SEP;
402+
sDefaultDir = Utils.getApp().getCacheDir() + FILE_SEP + "log" + FILE_SEP;
447403
}
448404
}
449405

450406
public Config setLogSwitch(final boolean logSwitch) {
451-
LogUtils.sLogSwitch = logSwitch;
407+
sLogSwitch = logSwitch;
452408
return this;
453409
}
454410

455411
public Config setConsoleSwitch(final boolean consoleSwitch) {
456-
LogUtils.sLog2ConsoleSwitch = consoleSwitch;
412+
sLog2ConsoleSwitch = consoleSwitch;
457413
return this;
458414
}
459415

460416
public Config setGlobalTag(final String tag) {
461417
if (isSpace(tag)) {
462-
LogUtils.sGlobalTag = "";
418+
sGlobalTag = "";
463419
sTagIsSpace = true;
464420
} else {
465-
LogUtils.sGlobalTag = tag;
421+
sGlobalTag = tag;
466422
sTagIsSpace = false;
467423
}
468424
return this;
469425
}
470426

471427
public Config setLogHeadSwitch(final boolean logHeadSwitch) {
472-
LogUtils.sLogHeadSwitch = logHeadSwitch;
428+
sLogHeadSwitch = logHeadSwitch;
473429
return this;
474430
}
475431

476432
public Config setLog2FileSwitch(final boolean log2FileSwitch) {
477-
LogUtils.sLog2FileSwitch = log2FileSwitch;
433+
sLog2FileSwitch = log2FileSwitch;
478434
return this;
479435
}
480436

481437
public Config setDir(final String dir) {
482438
if (isSpace(dir)) {
483-
LogUtils.dir = null;
439+
sDir = null;
484440
} else {
485-
LogUtils.dir = dir.endsWith(FILE_SEP) ? dir : dir + FILE_SEP;
441+
sDir = dir.endsWith(FILE_SEP) ? dir : dir + FILE_SEP;
486442
}
487443
return this;
488444
}
489445

490446
public Config setDir(final File dir) {
491-
LogUtils.dir = dir == null ? null : dir.getAbsolutePath() + FILE_SEP;
447+
sDir = dir == null ? null : dir.getAbsolutePath() + FILE_SEP;
448+
return this;
449+
}
450+
451+
public Config setFilePrefix(final String filePrefix) {
452+
if (isSpace(filePrefix)) {
453+
sFilePrefix = "util";
454+
} else {
455+
sFilePrefix = filePrefix;
456+
}
492457
return this;
493458
}
494459

495460
public Config setBorderSwitch(final boolean borderSwitch) {
496-
LogUtils.sLogBorderSwitch = borderSwitch;
461+
sLogBorderSwitch = borderSwitch;
497462
return this;
498463
}
499464

500465
public Config setConsoleFilter(@TYPE final int consoleFilter) {
501-
LogUtils.sConsoleFilter = consoleFilter;
466+
sConsoleFilter = consoleFilter;
502467
return this;
503468
}
504469

505470
public Config setFileFilter(@TYPE final int fileFilter) {
506-
LogUtils.sFileFilter = fileFilter;
471+
sFileFilter = fileFilter;
507472
return this;
508473
}
509474

@@ -514,7 +479,8 @@ public String toString() {
514479
+ LINE_SEP + "tag: " + (sTagIsSpace ? "null" : sGlobalTag)
515480
+ LINE_SEP + "head: " + sLogHeadSwitch
516481
+ LINE_SEP + "file: " + sLog2FileSwitch
517-
+ LINE_SEP + "dir: " + (dir == null ? defaultDir : dir)
482+
+ LINE_SEP + "dir: " + (sDir == null ? sDefaultDir : sDir)
483+
+ LINE_SEP + "filePrefix" + sFilePrefix
518484
+ LINE_SEP + "border: " + sLogBorderSwitch
519485
+ LINE_SEP + "consoleFilter: " + T[sConsoleFilter - V]
520486
+ LINE_SEP + "fileFilter: " + T[sFileFilter - V];

0 commit comments

Comments
 (0)