mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-22 14:41:08 -04:00
92 lines
3.9 KiB
Java
92 lines
3.9 KiB
Java
/*
|
|
* This file is part of Bitsquare.
|
|
*
|
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or (at
|
|
* your option) any later version.
|
|
*
|
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
|
* License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package io.bitsquare.app;
|
|
|
|
import ch.qos.logback.classic.Level;
|
|
import ch.qos.logback.classic.Logger;
|
|
import ch.qos.logback.classic.LoggerContext;
|
|
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
|
|
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
|
|
import ch.qos.logback.core.rolling.RollingFileAppender;
|
|
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class Log {
|
|
private static boolean PRINT_TRACE_METHOD = true;
|
|
private static SizeBasedTriggeringPolicy triggeringPolicy;
|
|
private static Logger logbackLogger;
|
|
|
|
public static void setup(String fileName, boolean useDetailedLogging) {
|
|
Log.PRINT_TRACE_METHOD = useDetailedLogging;
|
|
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
|
|
|
|
RollingFileAppender appender = new RollingFileAppender();
|
|
appender.setContext(loggerContext);
|
|
appender.setFile(fileName + ".log");
|
|
|
|
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
|
|
rollingPolicy.setContext(loggerContext);
|
|
rollingPolicy.setParent(appender);
|
|
rollingPolicy.setFileNamePattern(fileName + "_%i.log");
|
|
rollingPolicy.setMinIndex(1);
|
|
rollingPolicy.setMaxIndex(10);
|
|
rollingPolicy.start();
|
|
|
|
triggeringPolicy = new SizeBasedTriggeringPolicy();
|
|
triggeringPolicy.setMaxFileSize("1MB");
|
|
triggeringPolicy.start();
|
|
|
|
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
|
|
encoder.setContext(loggerContext);
|
|
encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n");
|
|
encoder.start();
|
|
|
|
appender.setEncoder(encoder);
|
|
appender.setRollingPolicy(rollingPolicy);
|
|
appender.setTriggeringPolicy(triggeringPolicy);
|
|
appender.start();
|
|
|
|
logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
|
|
//TODO for now use always trace
|
|
logbackLogger.setLevel(useDetailedLogging ? Level.TRACE : Level.INFO);
|
|
// logbackLogger.setLevel(useDetailedLogging ? Level.TRACE : Level.DEBUG);
|
|
logbackLogger.addAppender(appender);
|
|
}
|
|
|
|
public static void traceCall() {
|
|
if (LoggerFactory.getLogger(Log.class).isTraceEnabled()) {
|
|
StackTraceElement stackTraceElement = new Throwable().getStackTrace()[1];
|
|
String methodName = stackTraceElement.getMethodName();
|
|
if (methodName.equals("<init>"))
|
|
methodName = "Constructor ";
|
|
String className = stackTraceElement.getClassName();
|
|
LoggerFactory.getLogger(className).trace("Called: {}", methodName);
|
|
}
|
|
}
|
|
|
|
public static void traceCall(String message) {
|
|
if (LoggerFactory.getLogger(Log.class).isTraceEnabled()) {
|
|
StackTraceElement stackTraceElement = new Throwable().getStackTrace()[1];
|
|
String methodName = stackTraceElement.getMethodName();
|
|
if (methodName.equals("<init>"))
|
|
methodName = "Constructor ";
|
|
String className = stackTraceElement.getClassName();
|
|
LoggerFactory.getLogger(className).trace("Called: {} [{}]", methodName, message);
|
|
}
|
|
}
|
|
}
|