liveBook · Manning

Log

As explained in gRPC Microservices in Go

A log is a recorded event or message describing activity in a software system.

An application log records messages about service activity. In a live microservice system, logs help explain what happened when a problem occurs and reveal application-level anomalies, such as rising error rates. They complement metrics and traces: a metric can expose an anomaly, while related logs provide detail about activity at that time. section 9.3.7 section 9.4

Logs from multiple services are hard to read as one operation unless they share a filterable field. Include an operation’s distributed trace ID in each relevant entry so a logging backend can retrieve its messages together. Structured key-value fields, such as an order ID, provide another way to narrow a search. section 9.3.7

...
type serviceLogger struct {
    formatter log.JSONFormatter                                    #1
}
 
func (l serviceLogger) Format(entry *log.Entry) ([]byte, error) {
    span := trace.SpanFromContext(entry.Context)                   #2
    entry.Data["trace_id"] = span.SpanContext().TraceID().String()
    entry.Data["span_id"] = span.SpanContext().SpanID().String()
    //Below injection is Just to understand what Context has
    entry.Data["Context"] = span.SpanContext()
    return l.formatter.Format(entry)                               #3
}
...

This Go logrus formatter reads span data from the log entry’s context, adds trace and span IDs, and emits the entry through a JSON formatter. A log call must carry its context for the formatter to use that span. section 9.3.7

For a Go gRPC service, configure the formatter at startup and write contextual entries during operations such as CreateOrder. A cluster-level pipeline can collect logs with Fluent Bit, store them in Elasticsearch, and search or visualize them in Kibana. Log patterns can also support alerts. section 9.3.7 section 9.4

How gRPC Microservices in Go covers it

How deep it goes
Works through structured log fields and a context-aware Go formatter, then identifies a cluster logging pipeline.
Examples
Shows order-ID metadata, a CreateOrder log entry, and trace and span IDs in formatted output.
What it assumes
Assumes an implemented microservice application and introduces logrus logging with a simple example.
What you can do after
Apply it via logrus to produce filterable contextual logs, then search related entries in a logging backend.
How it connects
Connects logs to distributed tracing, metrics, observability, and alerting.

Also explained in

Compare how 14 books explain log →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.