Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Add tag logging example
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Feb 19, 2017
1 parent 9016002 commit 978e4fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ encouraged. In cases where the data is meaningful, consider [logging a custom ev

Use `Logger` as normal:

```elixir
```ruby
logger.info("My log message")

# My log message @metadata {"level": "info", "context": {...}}
Expand All @@ -128,6 +128,35 @@ Timber will never deviate from the public `::Logger` interface in *any* way.

</p></details>

<details><summary><strong>Tagging logs</strong></summary><p>

Need a quick and easy way to identify a log? Use tags!:

```ruby
logger.info(message: "My log message", tag: "tag")
# My log message @metadata {"level": "info", "tags": ["tag"], "context": {...}}
```

Multiple tags:

```ruby
logger.info(message: "My log message", tags: ["tag1", "tag2"])

# My log message @metadata {"level": "info", "tags": ["tag1", "tag2"], "context": {...}}
```

Using `ActiveSupport::TaggedLogging`? It works with that as well:

```ruby
logger.tagged("tag") do
logger.info(message: "My log message", tags: ["important", "slow"])
end

# My log message @metadata {"level": "info", "tags": ["tag"], "context": {...}}
```

</p></details>

<details><summary><strong>Custom events</strong></summary><p>

1. Log a structured Hash (simplest)
Expand Down
1 change: 1 addition & 0 deletions lib/timber/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def build_log_entry(severity, time, progname, msg)
level = SEVERITY_MAP.fetch(severity)
context_snapshot = CurrentContext.instance.snapshot
tags = extract_active_support_tagged_logging_tags
tags += [msg.delete(:tag)] if msg.is_a?(Hash) && msg.key?(:tag)
tags += msg.delete(:tags) if msg.is_a?(Hash) && msg.key?(:tags)
event = Events.build(msg)

Expand Down

0 comments on commit 978e4fe

Please sign in to comment.