Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

打印日志换行 #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,76 +35,76 @@

public class XLogSampleApplication extends Application {

public static Printer globalFilePrinter;
public static Printer globalFilePrinter;

private static final long MAX_TIME = 1000 * 60 * 60 * 24 * 2; // two days
private static final long MAX_TIME = 1000 * 60 * 60 * 24 * 2; // two days

@Override
public void onCreate() {
super.onCreate();
@Override
public void onCreate() {
super.onCreate();

initXlog();
}
initXlog();
}

/**
* Initialize XLog.
*/
private void initXlog() {
LogConfiguration config = new LogConfiguration.Builder()
.logLevel(BuildConfig.DEBUG ? LogLevel.ALL // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
: LogLevel.NONE)
.tag(getString(R.string.global_tag)) // Specify TAG, default: "X-LOG"
// .enableThreadInfo() // Enable thread info, disabled by default
// .enableStackTrace(2) // Enable stack trace info with depth 2, disabled by default
// .enableBorder() // Enable border, disabled by default
// .jsonFormatter(new MyJsonFormatter()) // Default: DefaultJsonFormatter
// .xmlFormatter(new MyXmlFormatter()) // Default: DefaultXmlFormatter
// .throwableFormatter(new MyThrowableFormatter()) // Default: DefaultThrowableFormatter
// .threadFormatter(new MyThreadFormatter()) // Default: DefaultThreadFormatter
// .stackTraceFormatter(new MyStackTraceFormatter()) // Default: DefaultStackTraceFormatter
// .borderFormatter(new MyBoardFormatter()) // Default: DefaultBorderFormatter
// .addObjectFormatter(AnyClass.class, // Add formatter for specific class of object
// new AnyClassObjectFormatter()) // Use Object.toString() by default
.addInterceptor(new BlacklistTagsFilterInterceptor( // Add blacklist tags filter
"blacklist1", "blacklist2", "blacklist3"))
// .addInterceptor(new WhitelistTagsFilterInterceptor( // Add whitelist tags filter
// "whitelist1", "whitelist2", "whitelist3"))
// .addInterceptor(new MyInterceptor()) // Add a log interceptor
.build();
/**
* Initialize XLog.
*/
private void initXlog() {
LogConfiguration config = new LogConfiguration.Builder()
.logLevel(BuildConfig.DEBUG ? LogLevel.ALL // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
: LogLevel.NONE)
.tag(getString(R.string.global_tag)) // Specify TAG, default: "X-LOG"
// .enableThreadInfo() // Enable thread info, disabled by default
// .enableStackTrace(2) // Enable stack trace info with depth 2, disabled by default
// .enableBorder() // Enable border, disabled by default
// .jsonFormatter(new MyJsonFormatter()) // Default: DefaultJsonFormatter
// .xmlFormatter(new MyXmlFormatter()) // Default: DefaultXmlFormatter
// .throwableFormatter(new MyThrowableFormatter()) // Default: DefaultThrowableFormatter
// .threadFormatter(new MyThreadFormatter()) // Default: DefaultThreadFormatter
// .stackTraceFormatter(new MyStackTraceFormatter()) // Default: DefaultStackTraceFormatter
// .borderFormatter(new MyBoardFormatter()) // Default: DefaultBorderFormatter
// .addObjectFormatter(AnyClass.class, // Add formatter for specific class of object
// new AnyClassObjectFormatter()) // Use Object.toString() by default
.addInterceptor(new BlacklistTagsFilterInterceptor( // Add blacklist tags filter
"blacklist1", "blacklist2", "blacklist3"))
// .addInterceptor(new WhitelistTagsFilterInterceptor( // Add whitelist tags filter
// "whitelist1", "whitelist2", "whitelist3"))
// .addInterceptor(new MyInterceptor()) // Add a log interceptor
.build();

Printer androidPrinter = new AndroidPrinter(); // Printer that print the log using android.util.Log
Printer filePrinter = new FilePrinter // Printer that print the log to the file system
.Builder(new File(getExternalCacheDir().getAbsolutePath(), "log").getPath()) // Specify the path to save log file
.fileNameGenerator(new DateFileNameGenerator()) // Default: ChangelessFileNameGenerator("log")
// .backupStrategy(new MyBackupStrategy()) // Default: FileSizeBackupStrategy(1024 * 1024)
// .cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME)) // Default: NeverCleanStrategy()
.flattener(new ClassicFlattener()) // Default: DefaultFlattener
.writer(new SimpleWriter() { // Default: SimpleWriter
@Override
public void onNewFileCreated(File file) {
super.onNewFileCreated(file);
final String header = "\n>>>>>>>>>>>>>>>> File Header >>>>>>>>>>>>>>>>" +
"\nDevice Manufacturer: " + Build.MANUFACTURER +
"\nDevice Model : " + Build.MODEL +
"\nAndroid Version : " + Build.VERSION.RELEASE +
"\nAndroid SDK : " + Build.VERSION.SDK_INT +
"\nApp VersionName : " + BuildConfig.VERSION_NAME +
"\nApp VersionCode : " + BuildConfig.VERSION_CODE +
"\n<<<<<<<<<<<<<<<< File Header <<<<<<<<<<<<<<<<\n\n";
appendLog(header);
}
})
.build();
Printer androidPrinter = new AndroidPrinter(); // Printer that print the log using android.util.Log
Printer filePrinter = new FilePrinter // Printer that print the log to the file system
.Builder(new File(getExternalCacheDir().getAbsolutePath(), "log").getPath()) // Specify the path to save log file
.fileNameGenerator(new DateFileNameGenerator()) // Default: ChangelessFileNameGenerator("log")
// .backupStrategy(new MyBackupStrategy()) // Default: FileSizeBackupStrategy(1024 * 1024)
// .cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME)) // Default: NeverCleanStrategy()
.flattener(new ClassicFlattener()) // Default: DefaultFlattener
.writer(new SimpleWriter() { // Default: SimpleWriter
@Override
public void onNewFileCreated(File file) {
super.onNewFileCreated(file);
final String header = "\n>>>>>>>>>>>>>>>> File Header >>>>>>>>>>>>>>>>" +
"\nDevice Manufacturer: " + Build.MANUFACTURER +
"\nDevice Model : " + Build.MODEL +
"\nAndroid Version : " + Build.VERSION.RELEASE +
"\nAndroid SDK : " + Build.VERSION.SDK_INT +
"\nApp VersionName : " + BuildConfig.VERSION_NAME +
"\nApp VersionCode : " + BuildConfig.VERSION_CODE +
"\n<<<<<<<<<<<<<<<< File Header <<<<<<<<<<<<<<<<\n\n";
appendLog(header);
}
})
.build();

XLog.init( // Initialize XLog
config, // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
androidPrinter, // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
filePrinter);
XLog.init( // Initialize XLog
config, // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
androidPrinter, // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
filePrinter);

// For future usage: partial usage in MainActivity.
globalFilePrinter = filePrinter;
// For future usage: partial usage in MainActivity.
globalFilePrinter = filePrinter;

// Intercept all logs(including logs logged by third party modules/libraries) and print them to file.
LibCat.config(true, filePrinter);
}
// Intercept all logs(including logs logged by third party modules/libraries) and print them to file.
LibCat.config(true, filePrinter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,70 +30,76 @@
*/
public class DefaultBorderFormatter implements BorderFormatter {

private static final char VERTICAL_BORDER_CHAR = '║';
private static final char VERTICAL_BORDER_CHAR = '║';

// Length: 100.
private static final String TOP_HORIZONTAL_BORDER =
"╔═════════════════════════════════════════════════" +
"══════════════════════════════════════════════════";
// Length: 100.
private static final String TOP_HORIZONTAL_BORDER =
"╔═════════════════════════════════════════════════" +
"══════════════════════════════════════════════════";

// Length: 99.
private static final String DIVIDER_HORIZONTAL_BORDER =
"╟─────────────────────────────────────────────────" +
"──────────────────────────────────────────────────";
// Length: 99.
private static final String DIVIDER_HORIZONTAL_BORDER =
"╟─────────────────────────────────────────────────" +
"──────────────────────────────────────────────────";

// Length: 100.
private static final String BOTTOM_HORIZONTAL_BORDER =
"╚═════════════════════════════════════════════════" +
"══════════════════════════════════════════════════";
// Length: 100.
private static final String BOTTOM_HORIZONTAL_BORDER =
"╚═════════════════════════════════════════════════" +
"══════════════════════════════════════════════════";

@Override
public String format(String[] segments) {
if (segments == null || segments.length == 0) {
return "";
}
@Override
public String format(String[] segments) {
if (segments == null || segments.length == 0) {
return "";
}

String[] nonNullSegments = new String[segments.length];
int nonNullCount = 0;
for (String segment : segments) {
if (segment != null) {
nonNullSegments[nonNullCount++] = segment;
}
}
if (nonNullCount == 0) {
return "";
}
String[] nonNullSegments = new String[segments.length];
int nonNullCount = 0;
for (String segment : segments) {
if (segment != null) {
nonNullSegments[nonNullCount++] = segment;
}
}
if (nonNullCount == 0) {
return "";
}

StringBuilder msgBuilder = new StringBuilder(" ");
msgBuilder
.append(SystemCompat.lineSeparator)
.append(TOP_HORIZONTAL_BORDER)
.append(SystemCompat.lineSeparator);
for (int i = 0; i < nonNullCount; i++) {
msgBuilder.append(appendVerticalBorder(nonNullSegments[i]))
.append(SystemCompat.lineSeparator);

if (i != nonNullCount - 1) {
msgBuilder.append(DIVIDER_HORIZONTAL_BORDER);
} else {
msgBuilder.append(BOTTOM_HORIZONTAL_BORDER);
}

StringBuilder msgBuilder = new StringBuilder();
msgBuilder.append(TOP_HORIZONTAL_BORDER).append(SystemCompat.lineSeparator);
for (int i = 0; i < nonNullCount; i++) {
msgBuilder.append(appendVerticalBorder(nonNullSegments[i]));
if (i != nonNullCount - 1) {
msgBuilder.append(SystemCompat.lineSeparator).append(DIVIDER_HORIZONTAL_BORDER)
.append(SystemCompat.lineSeparator);
} else {
msgBuilder.append(SystemCompat.lineSeparator).append(BOTTOM_HORIZONTAL_BORDER);
}
msgBuilder.append(SystemCompat.lineSeparator);
}
return msgBuilder.toString();
}
return msgBuilder.toString();
}

/**
* Add {@value #VERTICAL_BORDER_CHAR} to each line of msg.
*
* @param msg the message to add border
* @return the message with {@value #VERTICAL_BORDER_CHAR} in the start of each line
*/
private static String appendVerticalBorder(String msg) {
StringBuilder borderedMsgBuilder = new StringBuilder(msg.length() + 10);
String[] lines = msg.split(SystemCompat.lineSeparator);
for (int i = 0, N = lines.length; i < N; i++) {
if (i != 0) {
borderedMsgBuilder.append(SystemCompat.lineSeparator);
}
String line = lines[i];
borderedMsgBuilder.append(VERTICAL_BORDER_CHAR).append(line);
/**
* Add {@value #VERTICAL_BORDER_CHAR} to each line of msg.
*
* @param msg the message to add border
* @return the message with {@value #VERTICAL_BORDER_CHAR} in the start of each line
*/
private static String appendVerticalBorder(String msg) {
StringBuilder borderedMsgBuilder = new StringBuilder(msg.length() + 10);
String[] lines = msg.split(SystemCompat.lineSeparator);
for (int i = 0, N = lines.length; i < N; i++) {
if (i != 0) {
borderedMsgBuilder.append(SystemCompat.lineSeparator);
}
String line = lines[i];
borderedMsgBuilder.append(VERTICAL_BORDER_CHAR).append(line);
}
return borderedMsgBuilder.toString();
}
return borderedMsgBuilder.toString();
}
}