0% found this document useful (0 votes)
11 views10 pages

Logger Test

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Logger Test

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Log properties

public class LogProperties extends Properties {

private static final long serialVersionUID = -44545454545454L;

@Override
public String getProperty(String key) {
String property = super.getProperty(key);
if (property == null) {
property = key;
}
return property;
}

ServiceLogger class

import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.core.io.ClassPathResource;

@SuppressWarnings("nls")
public final class ServiceLogger {

private static final String LOG_PROPERTIES = "log_%s.properties";

private static final Properties LOG_MSGS = new LogProperties();


private static final String TYPE_OF_LOGS = "typeOfLog";
private static final String MESSAGE_CODE = "messageCode";
// Digits to identify the logs are originating from Databundle services
private static final String XXXXXBUNDLE_CODE = "20";

static {
readErrorMsgs();
}

private final Logger loggerInstance;


private final String msCode;
private final String packageCode;
private final String classlevelCode;

public ServiceLogger(
Class<?> containerClass,
String serviceCode,
String packageLogCode
)
{
loggerInstance = LoggerFactory.getLogger(containerClass);
msCode = serviceCode;
packageCode = packageLogCode;
classlevelCode = "";
}

public ServiceLogger(
Class<?> containerClass,
String serviceCode,
String packageLogCode,
String classCode
)
{
loggerInstance = LoggerFactory.getLogger(containerClass);
msCode = serviceCode;
packageCode = packageLogCode;
classlevelCode = classCode;
}

/**
* Read Errormegs from logProperties inputstream
*/
private static void readErrorMsgs() {
try (InputStream in = getLogPropertiesInputStream()) {
LOG_MSGS.load(in);
} catch (IOException e) {
LoggerFactory.getLogger(ServiceLogger.class).error("Error reading
properties", e);
}
}

/**
* get the log properties InputStream based on lang from resources
*
* @return InputStream
*
* @throws IOException
*/
private static InputStream getLogPropertiesInputStream() throws IOException {
String language = Locale.getDefault().getLanguage();
String filePath = String.format(LOG_PROPERTIES, language);
InputStream in;
try {
in = new ClassPathResource(filePath).getInputStream();
} catch (IOException e) {
in = new ClassPathResource(String.format(LOG_PROPERTIES,
"en")).getInputStream();
LoggerFactory.getLogger(ServiceLogger.class)
.debug("The local language log was not found: So going with
english.", e);
}
return in;
}

/**
* Log a message at the DEBUG level.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the message String to be logged
*/
public void debug(
LogType type,
int serialNumber,
String arg0
)
{
putMDC(type, serialNumber);
loggerInstance.debug(LOG_MSGS.getProperty(arg0));
}

/**
* Log a message at the DEBUG level according to the specified format and
argument.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 a list of 3 or more arguments
*/
public void debug(
LogType type,
int serialNumber,
String arg0,
Object... arg1
)
{
putMDC(type, serialNumber);
loggerInstance.debug(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log an exception (throwable) at the DEBUG level with an accompanying
message.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 the exception (throwable) to log
*/
public void debug(
LogType type,
int serialNumber,
String arg0,
Throwable arg1
)
{
putMDC(type, serialNumber);
loggerInstance.debug(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log a message at the ERROR level.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the message String to be logged
*/
public void error(
LogType type,
int serialNumber,
String arg0
)
{
putMDC(type, serialNumber);
loggerInstance.error(LOG_MSGS.getProperty(arg0));
}

/**
* Log a message at the ERROR level according to the specified format and
arguments.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 a list of 3 or more arguments
*/
public void error(
LogType type,
int serialNumber,
String arg0,
Object... arg1
)
{
putMDC(type, serialNumber);
loggerInstance.error(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log an exception (throwable) at the ERROR level with an accompanying
message.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 the exception (throwable) to log
*/
public void error(
LogType type,
int serialNumber,
String arg0,
Throwable arg1
)
{
putMDC(type, serialNumber);
loggerInstance.error(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log a message at the INFO level.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the message String to be logged
*/
public void info(
LogType type,
int serialNumber,
String arg0
)
{
putMDC(type, serialNumber);
loggerInstance.info(LOG_MSGS.getProperty(arg0));
}

/**
* Log a message at the INFO level according to the specified format and
arguments.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 a list of 3 or more arguments
*/
public void info(
LogType type,
int serialNumber,
String arg0,
Object... arg1
)
{
putMDC(type, serialNumber);
loggerInstance.info(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log an exception (throwable) at the INFO level with an accompanying message
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 the exception (throwable) to log
*/
public void info(
LogType type,
int serialNumber,
String arg0,
Throwable arg1
)
{
putMDC(type, serialNumber);
loggerInstance.info(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log a message at the TRACE level.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the message String to be logged
*/
public void trace(
LogType type,
int serialNumber,
String arg0
)
{
putMDC(type, serialNumber);
loggerInstance.trace(LOG_MSGS.getProperty(arg0));
}

/**
* Log a message at the TRACE level according to the specified format and
arguments.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 a list of 3 or more arguments
*/
public void trace(
LogType type,
int serialNumber,
String arg0,
Object... arg1
)
{
putMDC(type, serialNumber);
loggerInstance.trace(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log an exception (throwable) at the TRACE level with an accompanying
message.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 the exception (throwable) to log
*/
public void trace(
LogType type,
int serialNumber,
String arg0,
Throwable arg1
)
{
putMDC(type, serialNumber);
loggerInstance.trace(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log a message at the WARN level according to the specified format and
arguments.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the message String to be logged
*/
public void warn(
LogType type,
int serialNumber,
String arg0
)
{
putMDC(type, serialNumber);
loggerInstance.warn(LOG_MSGS.getProperty(arg0));
}

/**
* Log a message at the WARN level according to the specified format and
arguments.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 a list of 3 or more arguments
*/
public void warn(
LogType type,
int serialNumber,
String arg0,
Object... arg1
)
{
putMDC(type, serialNumber);
loggerInstance.warn(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Log a message at the WARN level according to the specified format * and
arguments.
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
* @param arg0 the format of the string to be logged
* @param arg1 the exception (throwable) to log
*/
public void warn(
LogType type,
int serialNumber,
String arg0,
Throwable arg1
)
{
putMDC(type, serialNumber);
loggerInstance.warn(LOG_MSGS.getProperty(arg0), arg1);
}

/**
* Puts type of log and message code into MDC
*
* @param type the typeOfLog
* @param serialNumber the unique serial number for every log statement
*/
private void putMDC(
LogType type,
int serialNumber
)
{
MDC.put(TYPE_OF_LOGS, type.toString());
MDC.put(
MESSAGE_CODE,
XXXXXBUNDLE_CODE + msCode + packageCode + classlevelCode +
String.format("%03d", serialNumber)
);
}

Log constant

public final class LogCodeConstants {

/**
* Digits representing microservice code
*/

public static final String MS_CODE = "10";

private LogCodeConstants() {
}

/**
* Representing each package code
*/

public static final class PackageCode {

public static final String XXXX_YYYYYYY_ADAPTER = "30";


public static final String XXXX_YYYYYYY_API = "31";

private PackageCode() {
}
}

/**
* Inner class constants, used for naming classes based on package
(COSYM_SIMULATION_ADAPTER) Note: Existing
* constants shouldn't be used, use new code for new class
*/

public static final class ClassMD {

public static final String ZZZZZ_CLASS_CODE = "01";


public static final String CCCCC_CLASS_CODE = "02";

private ClassMD() {

}
}

/**
* Inner class constants, used for naming classes based on package
(COSYM_SIMULATION_ADAPTER) Note: Existing
* constants shouldn't be used, use new code for new class
*/

public static final class ClassML {


public static final String AAAAAAAA_CLASS_CODE = "01";
public static final String BBBBBBBB_CLASS_CODE = "02";

private ClassML() {

}
}

/**
* Digits representing in each log number in a class
*/
public static final class LogCode {

public static final int LOG_CODE_SO001 = 1;


public static final int LOG_CODE_SO002 = 2;
public static final int LOG_CODE_SO003 = 3;
public static final int LOG_CODE_SO004 = 4;
public static final int LOG_CODE_SO005 = 5;
public static final int LOG_CODE_SO006 = 6;
public static final int LOG_CODE_SO007 = 7;
public static final int LOG_CODE_SO008 = 8;
public static final int LOG_CODE_SO009 = 9;
public static final int LOG_CODE_SO010 = 10;
public static final int LOG_CODE_SO011 = 11;
public static final int LOG_CODE_SO012 = 12;
public static final int LOG_CODE_SO013 = 13;
public static final int LOG_CODE_SO014 = 14;
public static final int LOG_CODE_SO015 = 15;
public static final int LOG_CODE_SO016 = 16;
public static final int LOG_CODE_SO017 = 17;
public static final int LOG_CODE_SO018 = 18;
public static final int LOG_CODE_SO019 = 19;
public static final int LOG_CODE_SO020 = 20;
public static final int LOG_CODE_SO021 = 21;
public static final int LOG_CODE_SO022 = 22;
public static final int LOG_CODE_SO023 = 23;
public static final int LOG_CODE_SO024 = 24;
public static final int LOG_CODE_SO025 = 25;
public static final int LOG_CODE_SO026 = 26;
public static final int LOG_CODE_SO027 = 27;
public static final int LOG_CODE_SO028 = 28;
public static final int LOG_CODE_SO029 = 29;
public static final int LOG_CODE_SO030 = 30;
public static final int LOG_CODE_SO031 = 31;
public static final int LOG_CODE_SO032 = 32;
public static final int LOG_CODE_SO033 = 33;

private LogCode() {
}
}

In class to be used
- Declare the service logger

private static final ServiceLogger LOGGER =


new ServiceLogger(ClassA.class, LogCodeConstants.MS_CODE,
LogCodeConstants.PackageCodeXXXXXX_IMPL,
LogCodeConstants.XXXXXX.YYYYYY_CLASS_CODE
);

inside the method

@Override
public YYYYYYY createXXXXYYYYY(sssss payload) {
LOGGER.info(LogType.TECH, LogCodeConstants.LogCode.LOG_CODE_SO003,
"CREATE_TEST_XXXXXXX_JOB");

You might also like