Skip to content

Commit

Permalink
Merge branch 'release/1.6.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas committed Dec 29, 2023
2 parents c3a9ba4 + ecbba2f commit ff49e0e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 59 deletions.
62 changes: 38 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# go-logger

![GoVersion](https://img.shields.io/github/go-mod/go-version/gildas/go-logger)
[![GoDoc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/gildas/go-logger)
[![License](https://img.shields.io/github/license/gildas/go-logger)](https://github.com/gildas/go-logger/blob/master/LICENSE)
[![GoDoc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/gildas/go-logger)
[![License](https://img.shields.io/github/license/gildas/go-logger)](https://github.com/gildas/go-logger/blob/master/LICENSE)
[![Report](https://goreportcard.com/badge/github.com/gildas/go-logger)](https://goreportcard.com/report/github.com/gildas/go-logger)

![master](https://img.shields.io/badge/branch-master-informational)
Expand Down Expand Up @@ -92,6 +92,7 @@ func (s Stuff) DoSomething(other *OtherStuff) error {
The call to `Record(key, value)` creates a new `Logger` object. So, they are like Russian dolls when it comes down to actually writing the log message to the output stream. In other words, `Record` objects are collected from their parent's `Logger` back to the original `Logger`.

For example:

```go
var Log = logger.Create("test")
var child = Log.Record("key1", "value1").Record("key2", "value2")
Expand All @@ -116,6 +117,7 @@ The `Child` method will create one `Logger` that has a `Record` containing a top
The `Records` method will create one `Logger` that has 2 keys (*key2* and *key3*) with their values.

For example, with these methods:

```go
var Log = logger.Create("test")
var child1 = Log.Child("topic", "scope", "key2", "value2", "key3", "value3")
Expand Down Expand Up @@ -161,6 +163,7 @@ var Log = logger.Create("myapp", &logger.FileStream{Path: "/path/to/myapp.log"},
```

A few notes:

- the `StackDriverStream` needs a `LogID` parameter or the value of the environment variable `GOOGLE_PROJECT_ID`. (see [Google's StackDriver documentation](https://godoc.org/cloud.google.com/go/logging#NewClient) for the description of that parameter).
- `NilStream` is a `Stream` that does not write anything, all messages are lost.
- `MultiStream` is a `Stream` than can write to several streams.
Expand All @@ -176,6 +179,7 @@ var Log = logger.Create("myapp",
NewRecord().Set("key", "value"),
)
```

### Setting the LevelSet

All `Stream` types, except `NilStream` and `MultiStream` can use a `LevelSet`. When set, `Record` objects that have a `Level` below the `LevelSet` are not written to the `Stream`. This allows to log only stuff above *WARN* for instance.
Expand Down Expand Up @@ -211,6 +215,7 @@ log.FilterLess()
### StackDriver Stream

If you plan to log to Google's StackDriver from a Google Cloud Kubernetes or a Google Cloud Instance, you do not need the StackDriver Stream and should use the Stdout Stream with the StackDriver Converter, since the standard output of your application will be captured automatically by Google to feed StackDriver:

```go
var Log = logger.Create("myapp", "gcp") // "google" or "googlecloud" are valid aliases
var Log = logger.Create("myapp", &logger.StdoutStream{Converter: &logger.StackDriverConverter{}})
Expand All @@ -219,27 +224,29 @@ var Log = logger.Create("myapp", &logger.StdoutStream{Converter: &logger.StackDr
To be able to use the StackDriver Stream from outside Google Cloud, you have some configuration to do first.

On your workstation, you need to get the key filename:

1. Authenticate with Google Cloud
```console
gcloud auth login
```
```console
gcloud auth login
```
2. Create a Service Account (`logger-account` is just an example of a service account name)
```console
gcloud iam service-acccount create logger-account
```
```console
gcloud iam service-acccount create logger-account
```
3. Associate the Service Account to the Project you want to use
```console
gcloud projects add-iam-policy-binding my-logging-project \
--member "serviceAccount:[email protected]" \
--role "roles/logging.logWriter"
```
```console
gcloud projects add-iam-policy-binding my-logging-project \
--member "serviceAccount:[email protected]" \
--role "roles/logging.logWriter"
```
4. Retrieve the key filename
```console
gcloud iam service-accounts keys create /path/to/key.json \
--iam-account [email protected]
```
```console
gcloud iam service-accounts keys create /path/to/key.json \
--iam-account [email protected]
```

You can either set the `GOOGLE_APPLICATION_CREDENTIAL` and `GOOGLE_PROJECT_ID` environment variables with the path of the obtained key and Google Project ID or provide them to the StackDriver stream:

```go
var log = logger.Create("myapp", &logger.StackDriverStream{})
```
Expand Down Expand Up @@ -272,7 +279,7 @@ func MyFunc() {
```

**Note**: Since this feature can be expensive to compute, it is turned of by default.
To turn it on, you need to either specify the option in the Stream object, set the environment variable `LOG_SOURCEINFO` to _true_. It is also turned on if the environment variable `DEBUG` is _true_.
To turn it on, you need to either specify the option in the Stream object, set the environment variable `LOG_SOURCEINFO` to *true*. It is also turned on if the environment variable `DEBUG` is *true*.

### Timing your funcs

Expand Down Expand Up @@ -320,6 +327,7 @@ The following convenience methods can be used when creating a `Logger` from anot
```go
var log = logger.CreateIfNil(OtherLogger, "myapp")
```

```go
var log = logger.Create("myapp", OtherLogger)
```
Expand All @@ -341,6 +349,7 @@ var log = logger.Must(logger.FromContext(context))
The `Logger` can redact records as needed by simply implementing the `logger.Redactable` interface in the data that is logged.

For example:

```go
type Customer {
ID uuid.UUID `json:"id"`
Expand Down Expand Up @@ -400,7 +409,7 @@ You can also write your own `Converter` by implementing the `logger.Converter` i

```go
type MyConverter struct {
// ...
// ...
}

func (converter *MyConverter) Convert(record Record) Record {
Expand All @@ -414,12 +423,13 @@ var Log = logger.Create("myapp", &logger.StdoutStream{Converter: &MyConverter{}}
## Standard Log Compatibility

To use a `Logger` with the standard go `log` library, you can simply call the `AsStandardLog()` method. You can optionally give a `Level`:

```go
package main

import (
"net/http"
"github.com/gildas/go-logger"
"github.com/gildas/go-logger"
)

func main() {
Expand All @@ -438,13 +448,14 @@ func main() {
```

You can also give an `io.Writer` to the standard `log` constructor:

```go
package main

import (
"log"
"net/http"
"github.com/gildas/go-logger"
"github.com/gildas/go-logger"
)

func main() {
Expand Down Expand Up @@ -498,18 +509,21 @@ func main() {
}
```

When the http request handler (_MyHandler_) starts, the following records are logged:
When the http request handler (*MyHandler*) starts, the following records are logged:

- `reqid`, contains the request Header X-Request-Id if present, or a random UUID
- `path`, contains the URL Path of the request
- `remote`, contains the remote address of the request
- The `topic` is set to "route" and the `scope` to the path of the request URL

When the http request handler (_MyHandler_) ends, the following additional records are logged:
When the http request handler (*MyHandler*) ends, the following additional records are logged:

- `duration`, contains the duration in seconds (**float64**) of the handler execution

## Environment Variables

The `Logger` can be configured completely by environment variables if needed. These are:

- `LOG_DESTINATION`, default: `StdoutStream`
The `Stream`s to write logs to. It can be a comma-separated list (for a `MultiStream`)
- `LOG_LEVEL`, default: *INFO*
Expand All @@ -527,7 +541,7 @@ The `Logger` can be configured completely by environment variables if needed. Th
- `DEBUG`, default: none
If set to "1", this will set the default level to filter to *DEBUG*

# Thanks
## Thanks

Special thanks to [@chakrit](https://github.com/chakrit) for his [chakrit/go-bunyan](https://github.com/chakrit/go-bunyan) that inspired me. In fact earlier versions were wrappers around his library.

Expand Down
28 changes: 18 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/gildas/go-logger
go 1.20

require (
cloud.google.com/go/logging v1.8.1
github.com/gildas/go-core v0.5.5
cloud.google.com/go/logging v1.9.0
github.com/gildas/go-core v0.5.6
github.com/gildas/go-errors v0.3.6
github.com/google/uuid v1.4.0
github.com/google/uuid v1.5.0
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.15.0
google.golang.org/api v0.152.0
google.golang.org/api v0.154.0
)

require (
Expand All @@ -18,6 +18,9 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
Expand All @@ -26,17 +29,22 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit ff49e0e

Please sign in to comment.