Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logs): configure log format #322

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/main/java/io/cryostat/core/log/LogConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed 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.
*/
package io.cryostat.core.log;

public class LogConfig {

private boolean includeTimestamp = true;
private boolean includeThreadId = true;
private boolean includeLogLevel = true;

public LogConfig() {
// Default constructor
}

// Copy constructor
public LogConfig(LogConfig other) {
this.includeTimestamp = other.includeTimestamp;
this.includeThreadId = other.includeThreadId;
this.includeLogLevel = other.includeLogLevel;
}

public boolean isIncludeTimestamp() {
return includeTimestamp;
}

public void setIncludeTimestamp(boolean includeTimestamp) {
this.includeTimestamp = includeTimestamp;
}

public boolean isIncludeThreadId() {
return includeThreadId;
}

public void setIncludeThreadId(boolean includeThreadId) {
this.includeThreadId = includeThreadId;
}

public boolean isIncludeLogLevel() {
return includeLogLevel;
}

public void setIncludeLogLevel(boolean includeLogLevel) {
this.includeLogLevel = includeLogLevel;
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/cryostat/core/log/LogLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed 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.
*/
package io.cryostat.core.log;

public enum LogLevel {
ERROR,
WARN,
INFO,
DEBUG,
TRACE
}
130 changes: 62 additions & 68 deletions src/main/java/io/cryostat/core/log/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,108 +17,102 @@

import org.slf4j.LoggerFactory;

public enum Logger {
INSTANCE;
public class Logger {

private org.slf4j.Logger logger = LoggerFactory.getLogger(Logger.class);
private org.slf4j.Logger logger;
private LogConfig logConfig;

public void error(String message) {
logger.error(message);
}
public static final Logger INSTANCE = new Logger();

public void error(String message, Object a) {
logger.error(message, a);
private Logger() {
this.logger = LoggerFactory.getLogger(Logger.class);
this.logConfig = new LogConfig();
}

public void error(String message, Object a, Object b) {
logger.error(message, a, b);
public void setLogConfig(LogConfig logConfig) {
this.logConfig = new LogConfig(logConfig);
}

public void error(String message, Object... args) {
logger.error(message, args);
log(LogLevel.ERROR, message, args);
}

public void error(Exception exception) {
logger.error("Exception thrown", exception);
}

public void warn(String message) {
logger.warn(message);
}

public void warn(String message, Object a) {
logger.warn(message, a);
}

public void warn(String message, Object a, Object b) {
logger.warn(message, a, b);
log(LogLevel.ERROR, "Exception thrown", exception);
}

public void warn(String message, Object... args) {
logger.warn(message, args);
log(LogLevel.WARN, message, args);
}

public void warn(Exception exception) {
logger.warn("Exception thrown", exception);
}

public void info(String message) {
logger.info(message);
}

public void info(String message, Object a) {
logger.info(message, a);
}

public void info(String message, Object a, Object b) {
logger.info(message, a, b);
log(LogLevel.WARN, "Exception thrown", exception);
}

public void info(String message, Object... args) {
logger.info(message, args);
log(LogLevel.INFO, message, args);
}

public void info(Exception exception) {
logger.info("Exception thrown", exception);
}

public void debug(String message) {
logger.debug(message);
}

public void debug(String message, Object a) {
logger.debug(message, a);
}

public void debug(String message, Object a, Object b) {
logger.debug(message, a, b);
log(LogLevel.INFO, "Exception thrown", exception);
}

public void debug(String message, Object... args) {
logger.debug(message, args);
log(LogLevel.DEBUG, message, args);
}

public void debug(Exception exception) {
logger.debug("Exception thrown", exception);
}

public void trace(String message) {
logger.trace(message);
}

public void trace(String message, Object a) {
logger.trace(message, a);
}

public void trace(String message, Object a, Object b) {
logger.trace(message, a, b);
log(LogLevel.DEBUG, "Exception thrown", exception);
}

public void trace(String message, Object... args) {
logger.trace(message, args);
log(LogLevel.TRACE, message, args);
}

public void trace(Exception exception) {
logger.trace("Exception thrown", exception);
log(LogLevel.TRACE, "Exception thrown", exception);
}

private void log(LogLevel level, String message, Object... args) {
String formattedMessage = formatMessage(level, message, args);
switch (level) {
case ERROR:
logger.error(formattedMessage);
break;
case WARN:
logger.warn(formattedMessage);
break;
case INFO:
logger.info(formattedMessage);
break;
case DEBUG:
logger.debug(formattedMessage);
break;
case TRACE:
logger.trace(formattedMessage);
break;
}
}

private String formatMessage(LogLevel level, String message, Object... args) {
StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("{");
formattedMessage
.append("\"timestamp\":\"")
.append(System.currentTimeMillis())
.append("\",");
formattedMessage.append("\"level\":\"").append(level.name()).append("\",");
formattedMessage.append("\"message\":\"").append(message).append("\"");
if (args.length > 0) {
formattedMessage.append(" [");
for (int i = 0; i < args.length; i++) {
formattedMessage.append(args[i]);
if (i < args.length - 1) {
formattedMessage.append(", ");
}
}
formattedMessage.append("]");
}
return formattedMessage.toString();
}
}
Loading