Skip to content

Commit

Permalink
Use default timestamp as default format
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
jecisc committed Nov 29, 2018
1 parent cf18e85 commit 3ee5303
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
66 changes: 35 additions & 31 deletions documentation/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,23 @@ If you with to know all the loggers of a kind, you can use:
By default, the preamble of the log will be a timestamp with a human readable format like this:

```
29 November 2018 00:06:30 : Test
2018-11-29T23:19:55.511775+01:00 : Test
```

But this format is configurable. The `timestampFormatBlock:` method can be use with a parameter that is a block taking a stream as parameter and the timestamp (instance of DateAndTime) and use that to write the preamble on the stream.

```Smalltalk
TinyLogger default timestampFormatBlock: [ :s :timestamp | s << timestamp asString ]
TinyLogger default
timestampFormatBlock: [ :aStream :timestamp |
timestamp asDate printOn: aStream.
aStream << ' '. "Cannot use #space because of Stdio streams"
timestamp asTime print24: true on: aStream ]
```

This will produce logs of this format:

```
2018-11-29T00:46:37.389775+01:00 : Test
29 November 2018 00:06:30 : Test
```

## Record with your logger
Expand All @@ -133,7 +137,7 @@ To record a single line log you can just use the method `record`:
This will produce a log like this with the default `timestampFormatBlock`:

```
29 November 2018 00:49:20 : This is a string to log
2018-11-29T23:19:55.511775+01:00 : This is a string to log
```

### Recording the execution of a task
Expand All @@ -147,13 +151,13 @@ self execute: [ 1 to: 5 do: [ :value | value asString record ] ] recordedAs: 'Ta
Will produce a log like this:

```
29 November 2018 00:56:20 : Begin: Task with only one nesting.
29 November 2018 00:56:20 : 1
29 November 2018 00:56:20 : 2
29 November 2018 00:56:20 : 3
29 November 2018 00:56:20 : 4
29 November 2018 00:56:20 : 5
29 November 2018 00:56:20 : End: Task with only one nesting.
2018-11-29T23:21:04.897775+01:00 : Begin: Task with only one nesting.
2018-11-29T23:21:04.900775+01:00 : 1
2018-11-29T23:21:04.902775+01:00 : 2
2018-11-29T23:21:04.904775+01:00 : 3
2018-11-29T23:21:04.906775+01:00 : 4
2018-11-29T23:21:04.908775+01:00 : 5
2018-11-29T23:21:04.909775+01:00 : End: Task with only one nesting.
```

It is also possible to nest them like that:
Expand All @@ -171,26 +175,26 @@ self execute: [
It will produce this kind of output:

```
29 November 2018 00:57:45 : Begin: My first nest
29 November 2018 00:57:45 : Begin: My second nest
29 November 2018 00:57:45 : 1
29 November 2018 00:57:45 : End: My second nest
29 November 2018 00:57:45 : Begin: My second nest
29 November 2018 00:57:45 : 1
29 November 2018 00:57:45 : 2
29 November 2018 00:57:45 : End: My second nest
29 November 2018 00:57:45 : Begin: My second nest
29 November 2018 00:57:45 : 1
29 November 2018 00:57:45 : 2
29 November 2018 00:57:45 : 3
29 November 2018 00:57:45 : End: My second nest
29 November 2018 00:57:45 : Begin: My second nest
29 November 2018 00:57:45 : 1
29 November 2018 00:57:45 : 2
29 November 2018 00:57:45 : 3
29 November 2018 00:57:45 : 4
29 November 2018 00:57:45 : End: My second nest
29 November 2018 00:57:45 : End: My first nest
2018-11-29T23:21:20.147775+01:00 : Begin: My first nest
2018-11-29T23:21:20.151775+01:00 : Begin: My second nest
2018-11-29T23:21:20.153775+01:00 : 1
2018-11-29T23:21:20.155775+01:00 : End: My second nest
2018-11-29T23:21:20.157775+01:00 : Begin: My second nest
2018-11-29T23:21:20.158775+01:00 : 1
2018-11-29T23:21:20.160775+01:00 : 2
2018-11-29T23:21:20.161775+01:00 : End: My second nest
2018-11-29T23:21:20.163775+01:00 : Begin: My second nest
2018-11-29T23:21:20.164775+01:00 : 1
2018-11-29T23:21:20.165775+01:00 : 2
2018-11-29T23:21:20.167775+01:00 : 3
2018-11-29T23:21:20.169775+01:00 : End: My second nest
2018-11-29T23:21:20.171775+01:00 : Begin: My second nest
2018-11-29T23:21:20.172775+01:00 : 1
2018-11-29T23:21:20.175775+01:00 : 2
2018-11-29T23:21:20.176775+01:00 : 3
2018-11-29T23:21:20.177775+01:00 : 4
2018-11-29T23:21:20.179775+01:00 : End: My second nest
2018-11-29T23:21:20.180775+01:00 : End: My first nest
```

## Use another logger than the global logger
Expand Down
9 changes: 4 additions & 5 deletions src/TinyLogger/TinyLogger.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ Examples
]
""Change the default timestamp format""
TinyLogger default timestampFormatBlock: [ :stream :timestamp | stream << timestamp asString ].
TinyLogger default
timestampFormatBlock:
[ :aStream :timestamp | timestamp asDate printOn: aStream. aStream << ' '. ""Cannot use #space because of Stdio streams"" timestamp asTime print24: true on: aStream ].
Internal Representation and Key Implementation Points.
--------------------
Expand Down Expand Up @@ -131,10 +133,7 @@ TinyLogger >> decreaseDepthLevel [

{ #category : #accessing }
TinyLogger >> defaultTimestampFormatBlock [
^ [ :aStream :timestamp |
timestamp asDate printOn: aStream.
aStream << ' '. "Cannot use #space because Stdio streams does not implements it."
timestamp asTime print24: true on: aStream ]
^ [ :aStream :timestamp | aStream << timestamp asString ]
]

{ #category : #accessing }
Expand Down

0 comments on commit 3ee5303

Please sign in to comment.