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

Optional SLF4J support without adding compile time dependency #1341

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<version>5.14.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.orientechnologies.common.log;

import java.util.logging.Level;
import java.util.logging.Logger;

public class OJulLogger implements OLogger {

@Override
public void log(String name, Level iLevel, String iMessage, Throwable iException,
Object... iAdditionalArgs) {
Logger log = Logger.getLogger(name);
if (log.isLoggable(iLevel)) {
final String msg = String.format(iMessage, iAdditionalArgs);
if (iException != null)
log.log(iLevel, msg, iException);
else
log.log(iLevel, msg);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ public class OLogManager {
private static final String DEFAULT_LOG = "com.orientechnologies";

private static final OLogManager instance = new OLogManager();
private OLogger loggerImpl;

protected OLogManager() {
try {
Class.forName("org.slf4j.Logger");
loggerImpl = new OSlf4JLogger();
}
catch(ClassNotFoundException e) {
loggerImpl = new OJulLogger();
}
}

public void setConsoleLevel(final String iLevel) {
Expand All @@ -35,14 +43,7 @@ public void setFileLevel(final String iLevel) {
public void log(final Object iRequester, final Level iLevel, String iMessage, final Throwable iException,
final Object... iAdditionalArgs) {
if (iMessage != null) {
final Logger log = iRequester != null ? Logger.getLogger(iRequester.getClass().getName()) : Logger.getLogger(DEFAULT_LOG);
if (log.isLoggable(iLevel)) {
final String msg = String.format(iMessage, iAdditionalArgs);
if (iException != null)
log.log(iLevel, msg, iException);
else
log.log(iLevel, msg);
}
loggerImpl.log(iRequester != null ? iRequester.getClass().getName() : DEFAULT_LOG, iLevel, iMessage, iException, iAdditionalArgs);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.orientechnologies.common.log;

import java.util.logging.Level;

public interface OLogger {


void log(final String name, final Level iLevel, String iMessage,
final Throwable iException, final Object... iAdditionalArgs);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.orientechnologies.common.log;

import java.util.logging.Level;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OSlf4JLogger implements OLogger {

@Override
public void log(String name, Level iLevel, String iMessage,
Throwable iException, Object... iAdditionalArgs) {
Logger logger = LoggerFactory.getLogger(name);

if (canLog(logger, iLevel)) {
String msg = String.format(iMessage, iAdditionalArgs);
logger.info(msg, iException);

}

}

private boolean canLog(Logger logger, Level iLevel) {
return (Level.INFO == iLevel && logger.isInfoEnabled()
|| Level.WARNING == iLevel && logger.isWarnEnabled()
|| Level.SEVERE == iLevel && logger.isErrorEnabled()
|| (Level.FINE == iLevel || Level.FINER == iLevel || Level.FINEST == iLevel) && logger.isDebugEnabled());
}
}