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

#28 SLF4J logging #89

Merged
merged 13 commits into from
Apr 3, 2015
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@
<artifactId>javax.el-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>compile</scope>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add <optional>true</optional> here, in case a user doesn't want to use SLF4J logging, this dependency won't be in his project

<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/org/takes/facets/slf4j/Slf4j.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.takes.facets.slf4j;

import java.util.logging.Level;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Slf4j target.
*
* <p>The class is immutable and thread-safe.
* @author Dmitry Zaytsev (dmitry.zaytsev@gmail.com)
* @version $Id$
* @since 0.11.2
*/
@EqualsAndHashCode(of = "logger")
@SuppressWarnings("PMD.LoggerIsNotStaticFinal")
final class Slf4j implements Target {
/**
* Default log level.
*/
public static final Level DEFAULT_LEVEL = Level.FINE;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the point of this constant? why can't you use Level.FINE where it's needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 reviewer suggested to store default log level inside wrapper
#89 (comment)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmzaytsev yes, that's right, but you already store it inside wrapper, at this.level. This constant is redundant. Just use Level.FINE where you need it


/**
* Log level.
*/
private final transient Level level;

/**
* Logger.
*/
private transient Logger logger;

/**
* Ctor.
* @param clazz Logger class
*/
Slf4j(final Class<?> clazz) {
this(clazz.getName());
}

/**
* Ctor.
* @param name Logger name
*/
Slf4j(final String name) {
this(name, Slf4j.DEFAULT_LEVEL);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, replace this line with this(name, Level.FINE)

}

/**
* Ctor.
* @param clazz Logger class
* @param lvl Log level
*/
Slf4j(final Class<?> clazz, final Level lvl) {
this(clazz.getName(), lvl);
}

/**
* Ctor.
* @param name Logger name
* @param lvl Log level
*/
Slf4j(final String name, final Level lvl) {
this.level = lvl;
this.logger = LoggerFactory.getLogger(name);
}

/**
* Log message.
* @param format Format string
* @param param Parameters
*/
@Override
public void log(final String format, final Object... param) {
if (Level.FINEST.equals(this.level)) {
this.logger.trace(format, param);
} else if (Level.FINE.equals(this.level)) {
this.logger.debug(format, param);
} else if (Level.INFO.equals(this.level)) {
this.logger.info(format, param);
} else if (Level.WARNING.equals(this.level)) {
this.logger.warn(format, param);
} else if (Level.SEVERE.equals(this.level)) {
this.logger.error(format, param);
} else {
throw new IllegalArgumentException("Unknown log level");
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/org/takes/facets/slf4j/Target.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.takes.facets.slf4j;

/**
* Log target.
*
* <p>All implementations of this interface must be immutable and thread-safe.
* @author Dmitry Zaytsev (dmitry.zaytsev@gmail.com)
* @version $Id$
* @since 0.11
*/
interface Target {
/**
* SLF4J parameterized logging.
* @param format Format string
* @param param Params
*/
void log(final String format, final Object... param);
}
93 changes: 93 additions & 0 deletions src/main/java/org/takes/facets/slf4j/TkLogged.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.takes.facets.slf4j;

import java.io.IOException;
import lombok.EqualsAndHashCode;
import org.takes.Response;
import org.takes.Take;

/**
* Logs Take.act() calls.
*
* <p>The class is immutable and thread-safe.
* @author Dmitry Zaytsev (dmitry.zaytsev@gmail.com)
* @version $Id$
* @since 0.11.2
*/
@EqualsAndHashCode(of = { "origin", "target" })
public final class TkLogged implements Take {
/**
* Original take.
*/
private final transient Take origin;

/**
* Log target.
*/
private final transient Target target;

/**
* Ctor.
* @param take Original
* @param trget Log target
*/
TkLogged(final Take take, final Target trget) {
this.target = trget;
this.origin = take;
}

/**
* Ctor.
* @param take Original
*/
public TkLogged(final Take take) {
this(take, new Slf4j(TkLogged.class));
}

/**
* Print itself.
* @return Response
* @throws IOException If fails
* @todo #101 I expect implementations of Response and Take
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have a bug/ticket already for this problem, right? Would be good to put a link to it, into this puzzle and explain exactly how you expect puzzle resolver to use this new feature we expect

* interfaces will be able convert itself to a loggable string but
* they don't have this feature.
* See details here https://github.com/yegor256/takes/issues/101
* We will use toConsole() in this way
* this.target.log("...", this.origin.toConsole(), resp.toConsole, ...)
*/
@Override
public Response act() throws IOException {
final long started = System.currentTimeMillis();
final Response resp = this.origin.act();
this.target.log(
"[{}] #act() return [{}] in [{}] ms",
this.origin,
resp,
System.currentTimeMillis() - started
);
return resp;
}
}
51 changes: 51 additions & 0 deletions src/main/java/org/takes/facets/slf4j/TsLogged.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.takes.facets.slf4j;

import java.io.IOException;
import org.takes.Request;
import org.takes.Take;
import org.takes.Takes;

/**
* Logs Takes.route() calls.
*
* <p>The class is immutable and thread-safe.
* @author Dmitry Zaytsev (dmitry.zaytsev@gmail.com)
* @version $Id$
* @todo #28:30min/DEV This class not implemented yet, but has to be.
* Please implement it like TkLogged and don't forget about unit tests.
* In addition, need to be implement classes BkLogged, FtLogged, PsLogged
* which will make loggable interfaces Back, Front, Pass. They should all be
* implemented also like TkLogged.
* @since 0.11.2
*/
public final class TsLogged implements Takes {
@Override
public Take route(final Request request) throws IOException {
throw new UnsupportedOperationException("not implemented yet");
}
}

43 changes: 43 additions & 0 deletions src/main/java/org/takes/facets/slf4j/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Slf4j.
*
* <p>Classes in this package is a collection of wrappers for basic interfaces,
* which will log basic parameters of methods called.
* This is how you implement it
* in your "take":
*
* <pre> new TsLogged(
* new TsFork(
* // my config...
* )
* );
*
* @author Dmitry Zaytsev (dmitry.zaystev@gmail.com)
* @version $Id$
* @since 0.11
*/
package org.takes.facets.slf4j;
Loading