Cloudopt-logger是一个基于Kotlin的可扩展和可配置的日志框架扩展,支持Java,Kotlin和Android。
在Java中是使用
添加依赖
compile 'net.cloudopt.logger:cloudopt-logger:1.0.1'
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'ch.qos.logback:logback-classic:1.2.3'
- 官网使用示例
public class TestJavaCase {
private Logger logger = Logger.Companion.getLogger(TestCase.class);
@Test
public void example1() {
logger.debug("Start init....");
logger.info("Operation successful!");
logger.warn("The value must be not nul.");
logger.error("Unable to acquire lock!");
}
@Test
public void example2() {
logger.info("Please Wait.... " + Colorer.INSTANCE.blue("100"));
logger.info("Please Wait.... " + Colorer.INSTANCE.yellow("200"));
logger.info("Please Wait.... " + Colorer.INSTANCE.red("300"));
}
@Test
public void example3() {
LoggerConfiguration configuration = new LoggerConfiguration();
configuration.setColor(false);
Logger.Companion.setConfiguration(configuration);
example1();
}
@Test
public void example4() {
LoggerConfiguration configuration = new LoggerConfiguration();
configuration.setDebugPrefix("DEBUG");
configuration.setInfoPrefix("INFO");
configuration.setWarnPrefix("WARN");
configuration.setErrorPrefix("ERROR");
Logger.Companion.setConfiguration(configuration);
example1();
}
}
- 测试使用示例
import com.google.common.base.Joiner;
import net.cloudopt.logger.Colorer;
import net.cloudopt.logger.Logger;
public class StringTest {
private static Logger logger = Logger.Companion.getLogger(StringTest.class);
public static void main(String[] args) {
// skipNulls() 跳过null
// useForNull("rep") 使用给定的字符替换null
Joiner joiner = Joiner.on("; ").skipNulls();
String str = joiner.join("Harry", null, "Ron", "Hermione");
logger.info(str);
logger.warn(str);
logger.debug(str);
logger.error(str);
// 自定义日志颜色
logger.info(Colorer.INSTANCE.red(str));
}
}
测试结果