de.bund.bva.isyfact.logging.util.LogHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of isy-logging Show documentation
Show all versions of isy-logging Show documentation
Stellt eine Komponente für das Logging bereit.
The newest version!
package de.bund.bva.isyfact.logging.util;
/*
* #%L
* isy-logging
* %%
*
* %%
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* The Federal Office of Administration (Bundesverwaltungsamt, BVA)
* licenses this file to you under the Apache License, Version 2.0 (the
* License). You may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
* #L%
*/
import java.lang.reflect.Method;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import de.bund.bva.isyfact.logging.IsyLogger;
import de.bund.bva.isyfact.logging.IsyMarker;
import de.bund.bva.isyfact.logging.LogKategorie;
import de.bund.bva.isyfact.logging.impl.Ereignisschluessel;
import de.bund.bva.isyfact.logging.impl.IsyMarkerImpl;
import de.bund.bva.isyfact.logging.impl.MarkerSchluessel;
/**
* Helper class for creating log entries. It provides the other helper classes of this component
* with a mechanism for creating uniform logs.
*
*/
public class LogHelper {
/** Flag determining whether a method invocation should be logged. */
private final boolean loggeAufruf;
/** Flag determining whether the result of an invocation (Success/Failure) should be logged. */
private final boolean loggeErgebnis;
/** Flag determining whether the duration of an invocation should be logged. */
private final boolean loggeDauer;
/** Flag determining whether all data passed to a method during its invocation should be logged. */
private final boolean loggeDaten;
/**
* Flag determining whether the parameters of a method invocation should be
* logged if an exception occurs.
*/
private final boolean loggeDatenBeiException;
/**
* Configuration property to set the maximum size limit for an invocation parameter to be included
* in the log.
*/
private final long loggeMaximaleParameterGroesse;
/** Converter to convert beans before they are serialized. */
private BeanConverter konverter;
/** Validator which determines the suitability of a bean to be logged as a parameter. */
private final BeanGroessePruefer pruefer;
/**
* Constructor of the class. Initializes the passed in class attributes.
*
* @param loggeDauer
* Flag determining whether the duration of an invocation should be logged.
* @param loggeAufruf
* Flag determining whether a method invocation should be logged.
* @param loggeErgebnis
* Flag determining whether the result of an invocation (Success/Failure) should be logged.
* @param loggeDaten
* Flag determining whether all data passed to a method during its invocation should be logged.
* @param loggeDatenBeiException
* Flag determining whether the parameters of a method invocation should be
* logged if an exception occurs.
* @param loggeMaximaleParameterGroesse
* Configuration property to set the maximum size limit for an invocation parameter to be included
* in the log.
*/
public LogHelper(boolean loggeAufruf, boolean loggeErgebnis, boolean loggeDauer,
boolean loggeDaten, boolean loggeDatenBeiException, long loggeMaximaleParameterGroesse) {
this(loggeAufruf, loggeErgebnis, loggeDauer, loggeDaten, loggeDatenBeiException,
loggeMaximaleParameterGroesse, null);
}
/**
* Constructor of the class. Initializes the passed in class attributes.
*
* @param loggeDauer
* Flag determining whether the duration of an invocation should be logged.
* @param loggeAufruf
* Flag determining whether a method invocation should be logged.
* @param loggeErgebnis
* Flag determining whether the result of an invocation (Success/Failure) should be logged.
* @param loggeDaten
* Flag determining whether all data passed to a method during its invocation should be logged.
* @param loggeDatenBeiException
* Flag determining whether the parameters of a method invocation should be
* logged if an exception occurs.
* @param loggeMaximaleParameterGroesse
* Configuration property to set the maximum size limit for an invocation parameter to be included
* in the log.
* @param konverter
* Converter to convert beans before they are serialized.
*/
public LogHelper(boolean loggeAufruf, boolean loggeErgebnis, boolean loggeDauer, boolean loggeDaten,
boolean loggeDatenBeiException, long loggeMaximaleParameterGroesse, BeanConverter konverter) {
this.loggeAufruf = loggeAufruf;
this.loggeErgebnis = loggeErgebnis;
this.loggeDauer = loggeDauer;
this.loggeDaten = loggeDaten;
this.loggeDatenBeiException = loggeDatenBeiException;
this.loggeMaximaleParameterGroesse = loggeMaximaleParameterGroesse;
this.pruefer = new BeanGroessePruefer();
if (konverter == null) {
this.konverter = erstelleStandardKonverter();
}
else {
this.konverter = konverter;
}
}
/**
* Helper method for creating a BeanToMapConverter in case no converter was provided during the
* invocation of the constructor.
*
* @return The converter that is to be used.
*/
public static BeanToMapConverter erstelleStandardKonverter() {
List includes = new ArrayList<>();
includes.add("de.bund.bva");
return new BeanToMapConverter(includes, null);
}
/**
* Creates a log entry for the invocation of the passed in method.
*
* @param logger
* The logger that is to be used.
* @param methode
* The invoked method.
*/
public void loggeAufruf(IsyLogger logger, Method methode) {
if (logger.isInfoEnabled() && loggeAufruf) {
logger.info(LogKategorie.JOURNAL, Ereignisschluessel.EISYLO01001.name(),
Ereignisschluessel.EISYLO01001.getNachricht(), erstelleMethodenname(methode),
erstelleSignatur(methode));
}
}
/**
* Creates a log entry for the result of the passed in method's invocation.
*
* @param logger
* The logger that is to be used.
* @param methode
* The invoked method.
* @param erfolgreich
* Determines whether the invocation was successful (No exceptions were thrown).
* @param parameter
* Parameters the method was invoked with.
* @param ergebnis
* The result of the method invocation (This can also be an exception).
*/
public void loggeErgebnis(IsyLogger logger, Method methode, boolean erfolgreich, Object[] parameter,
Object ergebnis) {
if (logger.isInfoEnabled() && loggeErgebnis) {
if (erfolgreich) {
logger.info(LogKategorie.METRIK, Ereignisschluessel.EISYLO01002.name(),
Ereignisschluessel.EISYLO01002.getNachricht(), erstelleMethodenname(methode),
erstelleSignatur(methode));
} else {
logger.info(LogKategorie.METRIK, Ereignisschluessel.EISYLO01003.name(),
Ereignisschluessel.EISYLO01003.getNachricht(), erstelleMethodenname(methode),
erstelleSignatur(methode));
}
}
// Outputs the data if
// either "loggeDaten" is set to true
// or the invocation was not successful and "loggeDatenBeiException" is set to true.
boolean loggeAufrufUndErgebnisdaten = loggeDaten || !erfolgreich && loggeDatenBeiException;
if (logger.isDebugEnabled() && loggeAufrufUndErgebnisdaten) {
List