Commit 1c5c41df by Amir Aharon

2.1.3 add date time to ILoggerConsoleImpl

parent 4eb0f339
...@@ -7,3 +7,4 @@ ...@@ -7,3 +7,4 @@
.project .project
.classpath .classpath
gradle/ gradle/
out/
\ No newline at end of file
### Microservice Framework in JAVA ### Microservice Framework in JAVA
## 2.1.3
- add date time to ILoggerConsoleImpl
## 2.1.2
- add config to okhttp client read/connect timeouts
"microservice.http.client.read.timeout.seconds" (30)
"microservice.http.client.connect.timeout.seconds" (10)
## 2.1.1 ## 2.1.1
- downgrade metrics influxdb to fit the reporter on iot jar - downgrade metrics influxdb to fit the reporter on iot jar
......
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '2.1.2' version '2.1.3'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
......
...@@ -2,6 +2,8 @@ package microservice.io.impl; ...@@ -2,6 +2,8 @@ package microservice.io.impl;
import microservice.io.iface.ILogger; import microservice.io.iface.ILogger;
import java.time.LocalDateTime;
/** /**
* Created by amir on 03/05/17. * Created by amir on 03/05/17.
*/ */
...@@ -16,6 +18,7 @@ public class ILoggerConsoleImpl implements ILogger { ...@@ -16,6 +18,7 @@ public class ILoggerConsoleImpl implements ILogger {
public static final String ANSI_PURPLE = "\u001B[35m"; public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m"; public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m"; public static final String ANSI_WHITE = "\u001B[37m";
public static final int INITIAL_LOG_LENGTH = 128;
String name; String name;
String prefix; String prefix;
...@@ -23,37 +26,47 @@ public class ILoggerConsoleImpl implements ILogger { ...@@ -23,37 +26,47 @@ public class ILoggerConsoleImpl implements ILogger {
public ILoggerConsoleImpl(String name) { public ILoggerConsoleImpl(String name) {
this.name = name; this.name = name;
this.prefix = "[" + name + "]: "; this.prefix = " [" + name + "]: ";
}
String getDateTimeString(){
return LocalDateTime.now().toString();
} }
@Override @Override
public void fatal(String msg) { public void fatal(String msg) {
System.out.println(ANSI_RED + prefix + msg + ANSI_RESET); System.out.println(formatLogMsg(ANSI_RED,msg));
}
private String formatLogMsg(String color,String msg) {
StringBuilder sb = new StringBuilder(INITIAL_LOG_LENGTH);
sb.append(color).append(getDateTimeString()).append(prefix).append(msg).append(ANSI_RESET);
return sb.toString();
} }
@Override @Override
public void error(String msg) { public void error(String msg) {
if (level.ordinal() >= EnumLevel.E_ERROR.ordinal()) System.out.println(ANSI_RED + prefix + msg + ANSI_RESET); if (level.ordinal() >= EnumLevel.E_ERROR.ordinal()) System.out.println(formatLogMsg(ANSI_RED,msg));
} }
@Override @Override
public void warning(String msg) { public void warning(String msg) {
if (level.ordinal() >= EnumLevel.E_WARNING.ordinal()) System.out.println(ANSI_YELLOW + prefix + msg + ANSI_RESET); if (level.ordinal() >= EnumLevel.E_WARNING.ordinal()) System.out.println(formatLogMsg(ANSI_YELLOW,msg));
} }
@Override @Override
public void info(String msg) { public void info(String msg) {
if (level.ordinal() >= EnumLevel.E_INFO.ordinal()) System.out.println(ANSI_BLUE + prefix + msg + ANSI_RESET); if (level.ordinal() >= EnumLevel.E_INFO.ordinal()) System.out.println(formatLogMsg(ANSI_BLUE,msg));
} }
@Override @Override
public void debug(String msg) { public void debug(String msg) {
if (level.ordinal() >= EnumLevel.E_DEBUG.ordinal()) System.out.println(ANSI_WHITE + prefix + msg + ANSI_RESET); if (level.ordinal() >= EnumLevel.E_DEBUG.ordinal()) System.out.println(formatLogMsg(ANSI_WHITE,msg));
} }
@Override @Override
public void trace(String msg) { public void trace(String msg) {
if (level.ordinal() >= EnumLevel.E_TRACE.ordinal()) System.out.println(ANSI_GREEN + prefix + msg + ANSI_RESET); if (level.ordinal() >= EnumLevel.E_TRACE.ordinal()) System.out.println(formatLogMsg(ANSI_GREEN,msg));
} }
@Override @Override
......
package microservice;
import microservice.io.iface.ILogger;
import microservice.io.impl.ILoggerConsoleImpl;
import org.junit.Test;
import java.time.LocalDateTime;
public class TestLogger {
public static final int ITERATIONS = 1000000;
@Test
public void testLocalDateTime(){
final long startTime = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++){
final String localDateTime = LocalDateTime.now().toString();
}
System.out.println(String.format("Test of %d Took: %d ms",ITERATIONS, System.currentTimeMillis() - startTime));
}
@Test
public void testLogger(){
ILoggerConsoleImpl logger = new ILoggerConsoleImpl("test");
logger.setLogLevel(ILogger.EnumLevel.E_TRACE);
logger.fatal("test fatal msg");
logger.error("test error msg");
logger.warning("test warning msg");
logger.info("test info msg");
logger.debug("test debug msg");
logger.trace("test trace msg");
logger.fatal("test fatal msg");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment