diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java |
Publish
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java')
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java new file mode 100644 index 00000000000..b41e195f6a7 --- /dev/null +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java @@ -0,0 +1,109 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.core; + +import org.osgi.service.log.LogEntry; +import org.osgi.service.log.LogListener; + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * @author <a href="mailto:vikasp@yahoo-inc.com">Vikas Panwar</a> + */ +class ConsoleLogListener implements LogListener { + + public static final int DEFAULT_LOG_LEVEL = Integer.MAX_VALUE; + private final ConsoleLogFormatter formatter; + private final PrintStream out; + private final int maxLevel; + + ConsoleLogListener(PrintStream out, String serviceName, String logLevel) { + this.out = out; + this.formatter = new ConsoleLogFormatter(getHostname(), getProcessId(), serviceName); + this.maxLevel = parseLogLevel(logLevel); + } + + @Override + public void logged(LogEntry entry) { + if (entry.getLevel() > maxLevel) { + return; + } + out.println(formatter.formatEntry(entry)); + } + + public static int parseLogLevel(String logLevel) { + if (logLevel == null || logLevel.isEmpty()) { + return DEFAULT_LOG_LEVEL; + } + if (logLevel.equalsIgnoreCase("OFF")) { + return Integer.MIN_VALUE; + } + if (logLevel.equalsIgnoreCase("ERROR")) { + return 1; + } + if (logLevel.equalsIgnoreCase("WARNING")) { + return 2; + } + if (logLevel.equalsIgnoreCase("INFO")) { + return 3; + } + if (logLevel.equalsIgnoreCase("DEBUG")) { + return 4; + } + if (logLevel.equalsIgnoreCase("ALL")) { + return Integer.MAX_VALUE; + } + try { + return Integer.valueOf(logLevel); + } catch (NumberFormatException e) { + // fall through + } + return DEFAULT_LOG_LEVEL; + } + + public static ConsoleLogListener newInstance() { + return new ConsoleLogListener(System.out, + System.getProperty("jdisc.logger.tag"), + System.getProperty("jdisc.logger.level")); + } + + static String getProcessId() { + // platform independent + String jvmName = ManagementFactory.getRuntimeMXBean().getName(); + if (jvmName != null) { + int idx = jvmName.indexOf('@'); + if (idx > 0) { + try { + return Long.toString(Long.valueOf(jvmName.substring(0, jvmName.indexOf('@')))); + } catch (NumberFormatException e) { + // fall through + } + } + } + + // linux specific + File file = new File("/proc/self"); + if (file.exists()) { + try { + return file.getCanonicalFile().getName(); + } catch (IOException e) { + return null; + } + } + + // fallback + return null; + } + + static String getHostname() { + try { + return InetAddress.getLocalHost().getCanonicalHostName(); + } catch (UnknownHostException e) { + return null; + } + } +} |