-
Notifications
You must be signed in to change notification settings - Fork 203
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
Changes from 10 commits
8716371
301b7bd
54ae505
1f4bb7c
ed23f4d
89d3e04
4621b9d
6b695d1
a178ed5
1442dd8
ad64b11
0f2cb76
9bbe514
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the point of this constant? why can't you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yegor256 reviewer suggested to store default log level inside wrapper There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
/** | ||
* 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, replace this line with |
||
} | ||
|
||
/** | ||
* 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"); | ||
} | ||
} | ||
} |
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); | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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"); | ||
} | ||
} | ||
|
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; |
There was a problem hiding this comment.
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