22
33import android .os .Environment ;
44import android .support .annotation .IntDef ;
5+ import android .support .annotation .NonNull ;
56import android .util .Log ;
67
78import org .json .JSONArray ;
1617import java .io .StringWriter ;
1718import java .lang .annotation .Retention ;
1819import java .lang .annotation .RetentionPolicy ;
20+ import java .text .Format ;
1921import java .text .SimpleDateFormat ;
2022import java .util .Date ;
2123import java .util .Formatter ;
@@ -67,15 +69,17 @@ public final class LogUtils {
6769 private static boolean sLogBorderSwitch = true ; // log边框开关,默认开
6870 private static int sLogFilter = V ; // log过滤器
6971
70- private static final String TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
71- private static final String LEFT_BORDER = "║ " ;
72- private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
73- private static final String LINE_SEPARATOR = System .getProperty ("line.separator" );
74- private static final int MAX_LEN = 4000 ;
72+ private static final String FILE_SEP = System .getProperty ("file.separator" );
73+ private static final String LINE_SEP = System .getProperty ("line.separator" );
74+ private static final String TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
75+ private static final String LEFT_BORDER = "║ " ;
76+ private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
77+ private static final int MAX_LEN = 4000 ;
7578
7679 private static final String NULL_TIPS = "Log with null object." ;
7780 private static final String NULL = "null" ;
7881 private static final String ARGS = "args" ;
82+ private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ());
7983
8084 private LogUtils () {
8185 throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -86,12 +90,22 @@ public static class Builder {
8690 public Builder () {
8791 if (Environment .MEDIA_MOUNTED .equals (Environment .getExternalStorageState ())
8892 && Utils .getContext ().getExternalCacheDir () != null )
89- dir = Utils .getContext ().getExternalCacheDir () + File . separator + "log" + File . separator ;
93+ dir = Utils .getContext ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
9094 else {
91- dir = Utils .getContext ().getCacheDir () + File . separator + "log" + File . separator ;
95+ dir = Utils .getContext ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
9296 }
9397 }
9498
99+ public Builder (@ NonNull String dir ) {
100+ LogUtils .dir = dir ;
101+ if (!dir .endsWith (FILE_SEP ))
102+ LogUtils .dir += FILE_SEP ;
103+ }
104+
105+ public Builder (@ NonNull File dir ) {
106+ LogUtils .dir = dir .getAbsolutePath () + FILE_SEP ;
107+ }
108+
95109 public Builder setLogSwitch (boolean logSwitch ) {
96110 LogUtils .sLogSwitch = logSwitch ;
97111 return this ;
@@ -249,7 +263,7 @@ private static String[] processContents(int type, String tag, Object... contents
249263 }
250264 String head = sLogHeadSwitch
251265 ? new Formatter ()
252- .format ("Thread: %s, %s(%s.java:%d)" + LINE_SEPARATOR ,
266+ .format ("Thread: %s, %s(%s.java:%d)" + LINE_SEP ,
253267 Thread .currentThread ().getName (),
254268 targetElement .getMethodName (),
255269 className ,
@@ -275,17 +289,17 @@ private static String[] processContents(int type, String tag, Object... contents
275289 .append ("]" )
276290 .append (" = " )
277291 .append (content == null ? NULL : content .toString ())
278- .append (LINE_SEPARATOR );
292+ .append (LINE_SEP );
279293 }
280294 body = sb .toString ();
281295 }
282296 }
283297 String msg = head + body ;
284298 if (sLogBorderSwitch ) {
285299 StringBuilder sb = new StringBuilder ();
286- String [] lines = msg .split (LINE_SEPARATOR );
300+ String [] lines = msg .split (LINE_SEP );
287301 for (String line : lines ) {
288- sb .append (LEFT_BORDER ).append (line ).append (LINE_SEPARATOR );
302+ sb .append (LEFT_BORDER ).append (line ).append (LINE_SEP );
289303 }
290304 msg = sb .toString ();
291305 }
@@ -313,7 +327,7 @@ private static String formatXml(String xml) {
313327 transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
314328 transformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" , "4" );
315329 transformer .transform (xmlInput , xmlOutput );
316- xml = xmlOutput .getWriter ().toString ().replaceFirst (">" , ">" + LINE_SEPARATOR );
330+ xml = xmlOutput .getWriter ().toString ().replaceFirst (">" , ">" + LINE_SEP );
317331 } catch (Exception e ) {
318332 e .printStackTrace ();
319333 }
@@ -365,31 +379,32 @@ private static void print(final int type, final String tag, String msg) {
365379 }
366380
367381 private static void print2File (final String tag , final String msg ) {
368- Date now = new Date ();
369- String date = new SimpleDateFormat ("MM-dd" , Locale .getDefault ()).format (now );
382+ Date now = new Date (System .currentTimeMillis ());
383+ String format = FORMAT .format (now );
384+ String date = format .substring (0 , 5 );
385+ String time = format .substring (6 );
370386 final String fullPath = dir + date + ".txt" ;
371387 if (!createOrExistsFile (fullPath )) {
372388 Log .e (tag , "log to " + fullPath + " failed!" );
373389 return ;
374390 }
375- String time = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ()).format (now );
376391 StringBuilder sb = new StringBuilder ();
377392 if (sLogBorderSwitch ) {
378- sb .append (TOP_BORDER ).append (LINE_SEPARATOR );
393+ sb .append (TOP_BORDER ).append (LINE_SEP );
379394 sb .append (LEFT_BORDER )
380395 .append (time )
381396 .append (tag )
382- .append (LINE_SEPARATOR )
397+ .append (LINE_SEP )
383398 .append (msg );
384- sb .append (BOTTOM_BORDER ).append (LINE_SEPARATOR );
399+ sb .append (BOTTOM_BORDER ).append (LINE_SEP );
385400 } else {
386401 sb .append (time )
387402 .append (tag )
388- .append (LINE_SEPARATOR )
403+ .append (LINE_SEP )
389404 .append (msg )
390- .append (LINE_SEPARATOR );
405+ .append (LINE_SEP );
391406 }
392- sb .append (LINE_SEPARATOR );
407+ sb .append (LINE_SEP );
393408 final String dateLogContent = sb .toString ();
394409 if (executor == null ) {
395410 executor = Executors .newSingleThreadExecutor ();
0 commit comments