Skip to content

Commit

Permalink
Add document for error reporting to README
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yomo committed Dec 30, 2021
1 parent e1d3b8b commit dd25cb3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,38 @@ logger.Info().OperationStart("foo", "bar").Msg("started")
logger.Debug().OperationContinue("foo", "bar").Msg("processing")
logger.Info().OperationEnd("foo", "bar").Msg("done")
```

#### Using Error Reporting
To report errors using StackDriver's Error Reporting tool, a log line needs to follow a separate log format described in the [Error Reporting](https://cloud.google.com/error-reporting/docs/formatting-error-messages) documentation.

A concrete example of error reporting format log is as follows:
```go
logger := zerodriver.NewProductionLogger(
zerodriver.WithServiceName("my service"),
zerodriver.WithReportAllErrors(),
)
```


#### Reporting errors manually
If you do not want every error to be reported, you can attach ErrorReport() to log call manually:
```go
logger.Error("An error to be reported!").ErrorReport(runtime.Caller(0))
```
※ When you use `zerodriver.WithReportAllErrors()`, the manually added error report will be duplicated.

※ Please keep in mind that ErrorReport needs a `ServiceContext` attached to the log entry. If you did not configure this using `WithServiceContext`, error reports will get attached using service name as `unknown`. To prevent this from happening, either configure your core or attach service context before (or when) using the logger:
```go
logger.Error().
ServiceContext().
ErrorReport(runtime.Caller(0).
Msg("An error message to be reported!")
```
Or permanently attach it to your logger
```go
logger := zerodriver.NewProductionLogger(zerodriver.WithServiceName("my service"))
logger.Error().
ErrorReport(runtime.Caller(0).
Msg("An error message to be reported!")
```

0 comments on commit dd25cb3

Please sign in to comment.