Commit 4ca34a66 by amir

version 2.0.6 add get/set logging level from web

parent ed59b412
group 'com.ipgallery.common'
version '2.0.5-services'
version '2.0.6-services'
apply plugin: 'java'
apply plugin: 'maven-publish'
......
......@@ -16,6 +16,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import microservice.MicroserviceApp;
import microservice.defs.Enums;
import microservice.io.iface.ILogger;
import microservice.services.CommonServices;
import microservice.io.iface.IConfiguration;
import microservice.io.impl.IMetricsFactoryImpl;
......@@ -32,7 +33,9 @@ public class MonitorHandler implements CommonServices.IMethodClass
public static final String MON_PREFIX = "_mon";
private static final String RELOAD = "_reload";
public static final String STAT = "_stat";
public static final String LOG_LEVEL = "_logLevel";
public static final String FIELD_NAME = "name";
public static final String LEVEL = "Level";
List<BaseHandler> containers = null;
protected ObjectMapper objMapper = new ObjectMapper();
......@@ -184,5 +187,37 @@ public class MonitorHandler implements CommonServices.IMethodClass
(msgCtx,orgService) -> {
printStats((RestContext)msgCtx);
}));
methodParamsList.add(new CommonServices.MethodParams(Enums.EnumServiceType.E_REST,
CommonServices.EnumRestCommands.E_READ,
"/" + MON_PREFIX + "/" + LOG_LEVEL,
(msgCtx,orgService) -> {
printLogLevel((RestContext)msgCtx);
}));
methodParamsList.add(new CommonServices.MethodParams(Enums.EnumServiceType.E_REST,
CommonServices.EnumRestCommands.E_CREATE,
"/" + MON_PREFIX + "/" + LOG_LEVEL,
(msgCtx,orgService) -> {
setLogLevel((RestContext)msgCtx);
}));
}
private void setLogLevel(RestContext reqCtx) {
final JsonNode jsonNode = (JsonNode) reqCtx.container.readObjectFromRequest(reqCtx.request, JsonNode.class);
if (jsonNode != null){
String levelStr = jsonNode.path(LEVEL).asText();
MicroserviceApp.getsInstance().getLogger().setLogLevel(ILogger.EnumLevel.getLevel(levelStr));
}
printLogLevel(reqCtx);
}
private void printLogLevel(RestContext reqCtx) {
BaseRestResponse brr = new BaseRestResponse(true, null);
final ILogger logger = MicroserviceApp.getsInstance().getLogger();
if (logger.getLogLevel() != null)
brr.objectNode = JsonNodeFactory.instance.objectNode().put(LEVEL,logger.getLogLevel().getLevelStr());
reqCtx.container.writeObjectToResponse(reqCtx.response, brr);
}
}
package microservice.io.iface;
import java.util.Optional;
import java.util.stream.Stream;
/**
* logger interface to use in the MS
* @author amir
......@@ -7,10 +10,40 @@ package microservice.io.iface;
*/
public interface ILogger
{
enum EnumLevel {
E_FATAL("fatal"),
E_ERROR("error"),
E_WARNING("warning"),
E_INFO("info"),
E_DEBUG("debug"),
E_TRACE("trace");
String levelStr;
EnumLevel(String levelStr) {
this.levelStr = levelStr;
}
public String getLevelStr() {
return levelStr;
}
public static EnumLevel getLevel(String levelStr){
for (EnumLevel enumLevel : EnumLevel.values()){
if (enumLevel.getLevelStr().equals(levelStr))
return enumLevel;
}
return null;
}
}
public void fatal(String msg);
public void error(String msg);
public void warning(String msg);
public void info(String msg);
public void debug(String msg);
public void trace(String msg);
public EnumLevel getLogLevel();
public void setLogLevel(EnumLevel level);
}
......@@ -61,4 +61,14 @@ public class ILogger4jImpl implements ILogger
}
@Override
public EnumLevel getLogLevel() {
return null;
}
@Override
public void setLogLevel(EnumLevel level) {
}
}
......@@ -19,6 +19,7 @@ public class ILoggerConsoleImpl implements ILogger {
String name;
String prefix;
EnumLevel level = EnumLevel.E_INFO; // default level
public ILoggerConsoleImpl(String name) {
this.name = name;
......@@ -32,26 +33,37 @@ public class ILoggerConsoleImpl implements ILogger {
@Override
public void error(String msg) {
System.out.println(ANSI_RED + prefix + msg + ANSI_RESET);
if (level.ordinal() >= EnumLevel.E_ERROR.ordinal()) System.out.println(ANSI_RED + prefix + msg + ANSI_RESET);
}
@Override
public void warning(String msg) {
System.out.println(ANSI_YELLOW + prefix + msg + ANSI_RESET);
if (level.ordinal() >= EnumLevel.E_WARNING.ordinal()) System.out.println(ANSI_YELLOW + prefix + msg + ANSI_RESET);
}
@Override
public void info(String msg) {
System.out.println(ANSI_BLUE + prefix + msg + ANSI_RESET);
if (level.ordinal() >= EnumLevel.E_INFO.ordinal()) System.out.println(ANSI_BLUE + prefix + msg + ANSI_RESET);
}
@Override
public void debug(String msg) {
System.out.println(ANSI_WHITE + prefix + msg + ANSI_RESET);
if (level.ordinal() >= EnumLevel.E_DEBUG.ordinal()) System.out.println(ANSI_WHITE + prefix + msg + ANSI_RESET);
}
@Override
public void trace(String msg) {
System.out.println(ANSI_GREEN + prefix + msg + ANSI_RESET);
if (level.ordinal() >= EnumLevel.E_TRACE.ordinal()) System.out.println(ANSI_GREEN + prefix + msg + ANSI_RESET);
}
@Override
public EnumLevel getLogLevel() {
return level;
}
@Override
public void setLogLevel(EnumLevel level) {
if (level != null)
this.level = level;
}
}
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