net.welen.jmole.protocols.logger.Logger Maven / Gradle / Ivy
package net.welen.jmole.protocols.logger;
/*
* #%L
* JMole, https://bitbucket.org/awelen/jmole
* %%
* Copyright (C) 2015 - 2020 Anders Welén, [email protected]
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import net.welen.jmole.JMole;
import net.welen.jmole.presentation.PresentationInformation;
import net.welen.jmole.protocols.AbstractIntervalProtocol;
import net.welen.jmole.protocols.Utils;
public class Logger extends AbstractIntervalProtocol implements LoggerMBean {
private final static java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(Logger.class.getName());
private static final char SEP = '/';
private static String PROPERTY_LOGGER_NAME = "jmole.protocol.logger.name";
private static String PROPERTY_LOGGER_FORMAT = "jmole.protocol.logger.format";
private static String PROPERTY_LOGGER_LEVEL = "jmole.protocol.logger.level";
private static String PROPERTY_LOGGER_INTERVAL = "jmole.protocol.logger.interval";
private Level level;
private String format;
private Long interval;
private java.util.logging.Logger protocolLogger = java.util.logging.Logger.getLogger("JMole");
private JMole jmole;
@Override
public void startProtocol(JMole jmole) throws Exception {
this.jmole = jmole;
String levelString = System.getProperty(PROPERTY_LOGGER_LEVEL);
if (levelString == null) {
levelString = "INFO";
}
level = Level.parse(levelString);
String name = System.getProperty(PROPERTY_LOGGER_NAME);
if (name != null) {
protocolLogger = java.util.logging.Logger.getLogger(name);
}
format = System.getProperty(PROPERTY_LOGGER_FORMAT);
if (format == null) {
format = "[%k] %a [%A] = %v %U";
}
interval = Long.getLong(PROPERTY_LOGGER_INTERVAL);
if (interval == null) {
interval = 60000L;
}
super.startProtocol(jmole);
LOG.log(Level.INFO, "JMole Logger protocol started: Level=" + levelString + ", Interval=" + interval);
}
@Override
public void stopProtocol() throws Exception {
LOG.log(Level.INFO, "Stopping JMole Logger protocol");
super.stopProtocol();
LOG.log(Level.INFO, "JMole Logger protocol stopped");
}
@Override
public String getLevel() {
return level.getName();
}
@Override
public void setLevel(String level) {
this.level = Level.parse(level);
}
@Override
public long getInterval() {
return interval;
}
@Override
public void setInterval(long interval) {
this.interval = interval;
}
@Override
public String getFormat() {
return format;
}
@Override
public void setFormat(String format) {
this.format = format;
}
@Override
protected void handleMeasurement(String category, String name, String attribute, Object value, PresentationInformation presentationInformation) throws Exception {
protocolLogger.log(level, Utils.formatLogString(format, SEP, category, name, attribute, value, presentationInformation));
}
@Override
protected void handleWarnings() throws Exception {
for (Entry> categoryEntry : jmole.warningMessages().entrySet()) {
for (Entry entry : categoryEntry.getValue().entrySet()) {
protocolLogger.log(Level.WARNING, "[" + categoryEntry.getKey() + SEP + entry.getKey() + "] " + entry.getValue());
}
}
}
@Override
protected void handleCriticals() throws Exception {
for (Entry> categoryEntry : jmole.criticalMessages().entrySet()) {
for (Entry entry : categoryEntry.getValue().entrySet()) {
protocolLogger.log(Level.SEVERE, "[" + categoryEntry.getKey() + SEP + entry.getKey() + "] " + entry.getValue());
}
}
}
}