Skip to content

Commit 98df04e

Browse files
isidoreScottBob
andcommitted
F Expose toggles for SimpleLogger
Add use descriptions Co-Authored-By: Scott Wierschem <[email protected]>
1 parent 920a56a commit 98df04e

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

approvaltests-util-tests/src/test/java/com/spun/util/logger/SimpleLoggerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ void testMarkers()
3636
Approvals.verify(log);
3737
// end-snippet
3838
}
39+
40+
@Test
41+
void testToggles() {
42+
// begin-snippet: simple_logger_toggles
43+
SimpleLogger.get().marker = true;
44+
SimpleLogger.get().event = false;
45+
SimpleLogger.get().variable = false;
46+
SimpleLogger.get().query = true;
47+
// end-snippet
48+
}
49+
3950
public void sample()
4051
{
4152
// begin-snippet: log_nothing
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<a id="top"></a>
2+
3+
# How to do Performance Profiling using SimpleLogger
4+
5+
toc
6+
7+
## Understanding timings in log output
8+
When SimpleLogger logs to a file or Standard.out it includes timings (these are turned off by default if you are logging
9+
to a string as they are inconsistent for testing purposes.)
10+
11+
Here's an example of the output:
12+
```.text
13+
[May 20, 2021, 9:44:17 PM ~000002ms] => Sample.methodThatLogs() - IN
14+
[May 20, 2021, 9:44:17 PM ~000047ms] => Sample.innerMethod() - IN
15+
[May 20, 2021, 9:44:17 PM ~000001ms] Variable: i = '0'
16+
[May 20, 2021, 9:44:17 PM ~000000ms] <= Sample.innerMethod() - OUT
17+
```
18+
The important part here is `~000047ms` which represents the number of milliseconds that have passed since the previous line SimpleLogger
19+
logged. This allows an easy way to detect bottlenecks by adding log statements and test improvements.

approvaltests-util/docs/reference/SimpleLogger.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ for test purposes, rather than setting a verbosity level. For example, you could
1616
or "Show me all the method entries". Also, because SimpleLogger is tagging based on the event type, it has many
1717
convenience functions to properly format and call based on that format.
1818

19+
### Differences vs. System.out
20+
* **Single point of control**
21+
SimpleLogger puts a single place where you can collect all log statements. From here it is trivial to log to a file or
22+
string or both. SimpleLogger also allows you to easily turn off types of logging allowing you to control the
23+
signal-to-noise ratio.
24+
* **Convenience functions for metadata**
25+
SimpleLogger adds formatting and class names making logs consistent and robust after changing method or class names.
26+
This helps with both the writing and the reading of the logs.
27+
* **Time stamps**
28+
SimpleLogger automatically adds timestamps plus time differences to easily detected when a problem happened and where a performance bottleneck is
29+
### Differences vs. Log4J
30+
* **Don't need to hold on to logging objects**
31+
The common patter for Log4J is that every class has a field named `LOG`
32+
This tends to clutter up your files. SimpleLogger does not do this, opting instead to make all of its calls static.
33+
* **Finer control over turning off noise**
34+
Log4J will allow you to set a level of output. In practice, this usually means everything, or just warnings.
35+
SimpleLogger, instead, allows you to toggle individual event types.
36+
For example, the following would allow you to turn on only the queries and where they came from:
37+
snippet: simple_logger_toggles
38+
* **Can be used concurrently with SimpleLogger**
39+
There is nothing that prevents using both simultaneously
40+
### Logging method calls
41+
1942
For example, here's how you would log entry and exit from a method:
2043
<!-- snippet: simple_logger_use_markers -->
2144
<a id='snippet-simple_logger_use_markers'></a>
@@ -62,6 +85,7 @@ Will produce the following logs:
6285

6386
## HowTos
6487
* [How to capture logs for testing](../how_to/CaptureLogs.md#top)
88+
* [How to do performance profiling](../how_to/PerformanceProfilingWithSimpleLogger.md#top)
6589

6690
## SimpleLogger.logToNothing()
6791

approvaltests-util/src/main/java/com/spun/util/logger/SimpleLogger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,8 @@ public static Appendable getLogTo()
121121
{
122122
return log.getLogTo();
123123
}
124+
125+
public static SimpleLoggerInstance get() {
126+
return log;
127+
}
124128
}

0 commit comments

Comments
 (0)