Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc66b969

Browse files
committed
Added some extra examples to slf4j post
1 parent4e5391f commitc66b969

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

‎stubbornjava-webapp/ui/src/posts/logging-in-java-with-slf4j-and-logback.hbs‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
<h3class="anchored">What if I need more configurations?</h3>
1717
<p>Logback gives you some additional options for passing in the configurations. Let's say we have a single Jar that has many main methods inside of it. Maybe one or more services and potentially some mains that are run as cron jobs. In this case <code>src/main/resources/logback.xml</code> works for our services but we want a different config file for the cron jobs. Simply make a <code>src/main/resources/cron-logback.xml</code> and when you start the java process add the system property flag <nobr><code>-Dlogback.configurationFile=logback-crons.xml</code></nobr>. This will force logback to use that specific file instead of the default.</p>
1818

19-
<h3class="anchored">Usage</h3>
20-
{{>templates/src/widgets/code/code-snippetfile=logbacksection=logback.sections.logback}}
19+
<h3class="anchored">SLF4J Basic Logging Example</h3>
20+
21+
{{>templates/src/widgets/code/code-snippetfile=logbacksection=logback.sections.slf4j}}
2122

2223
<preclass="line-numbers"><codeclass="language-bash">2017-02-05 12:05:06.488 [main] INFO c.s.examples.logback.LogbackExamples - INFO
2324
2017-02-05 12:05:06.492 [main] DEBUG c.s.examples.logback.LogbackExamples - DEBUG
@@ -27,5 +28,35 @@
2728
2017-02-05 12:05:06.492 [main] ERROR c.s.secrets.MySecretPasswordClass - ERROR</code></pre>
2829
<p>Notice how neither logger logged at Trace level and MySecretPasswordClass only traced Warn and above.</p>
2930

31+
<h3class="anchored">SLF4J Message Formatter Example</h3>
32+
SLF4J offers some basic string formatting for convenience as well as a minor performance gain. A log message can start with a format followed by var args of <code>Object</code> to be passed into the message format. This also helps prevent unnecessary string concatenation for log messages that are not being hit.
33+
{{>templates/src/widgets/code/code-snippetfile=logbacksection=logback.sections.slf4jFormat}}
34+
<preclass="line-numbers"><codeclass="language-bash">2017-11-14 23:49:25.152 [main] INFO c.s.examples.logback.LogbackExamples - Hello world
35+
2017-11-14 23:49:25.156 [main] INFO c.s.examples.logback.LogbackExamples - Hello world i=0
36+
2017-11-14 23:49:25.156 [main] INFO c.s.examples.logback.LogbackExamples - Hello world i=1
37+
2017-11-14 23:49:25.156 [main] INFO c.s.examples.logback.LogbackExamples - Hello world i=2
38+
2017-11-14 23:49:25.157 [main] INFO c.s.examples.logback.LogbackExamples - Hello world i=3
39+
2017-11-14 23:49:25.157 [main] INFO c.s.examples.logback.LogbackExamples - Hello world i=4</code></pre>
40+
41+
42+
<h3class="anchored">SLF4J Conditional Logging Example</h3>
43+
Message formats give us the performance gain of no unnecessary string concatenation but what if we have very verbose logging? SLF4J allows you to check if a log level is enabled. Wrap this check in an if statement and you can now add very verbose logging that will not hinder performance more than a single conditional check if the logger is disabled.
44+
{{>templates/src/widgets/code/code-snippetfile=logbacksection=logback.sections.slf4jConditionalLogging}}
45+
<preclass="line-numbers"><codeclass="language-bash">2017-11-14 23:49:25.157 [main] INFO c.s.examples.logback.LogbackExamples - Logger expensive call null
46+
</code></pre>
47+
48+
<h3class="anchored">SLF4J Exception Logging Example</h3>
49+
Pretty much every method signature in SLF4J allows you to pass in a <code>Throwable</code> as a final parameter which will print out the stack trace and message along with your standard logging message. Remember to always add useful information to the logs. If an error occurred log the input or bad output, maybe some ids involved.
50+
51+
{{>templates/src/widgets/code/code-snippetfile=logbacksection=logback.sections.slf4jException}}
52+
<preclass="line-numbers"><codeclass="language-bash">2017-11-14 23:49:25.161 [main] WARN c.s.examples.logback.LogbackExamples - Something bad happened
53+
java.lang.RuntimeException: What happened?
54+
at com.stubbornjava.examples.logback.LogbackExamples.logException(LogbackExamples.java:60)
55+
at com.stubbornjava.examples.logback.LogbackExamples.main(LogbackExamples.java:72)
56+
2017-11-14 23:49:25.162 [main] WARN c.s.examples.logback.LogbackExamples - Something bad happened with id: 1
57+
java.lang.RuntimeException: What happened?
58+
at com.stubbornjava.examples.logback.LogbackExamples.logException(LogbackExamples.java:60)
59+
at com.stubbornjava.examples.logback.LogbackExamples.main(LogbackExamples.java:72)</code></pre>
60+
3061
<h3class="anchored">Monitoring</h3>
3162
<p>Often times even with proper logging your application can be spewing errors / exceptions in production and no one will notice for extended periods of time. Utilizing <ahref="/posts/monitoring-your-jvm-with-dropwizard-metrics#logging-level-hit-counts">Dropwizard Metrics SLF4J Instrumented Appender</a> you can easiy set up graphs or alerts on warn / error rates.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp