Go adds structured logging to its standard library…but should it?
Tags: go, logging, opensource, programming
Published: 2023-03-31 11:46:35

_Originally published at [https://sagikazarmark.hu](https://sagikazarmark.hu/blog/go-adds-structured-logging-to-its-stdlib/)._

---

The Go programming language has recently [accepted](https://github.com/golang/go/issues/56345#issuecomment-1470506816) a [proposal](https://go.googlesource.com/proposal/+/master/design/56345-structured-logging.md) to add a structured logging package to its standard library. This long-awaited addition is expected to land in a future version of the language, sparking excitement and debate among developers.

Before diving into the pros and cons of this decision, let's first take a look at the history of structured logging in Go.

## Structured logging in Go so far

Structured logging has been a topic of discussion in the Go community for quite some time. As the language gained popularity and more developers started using it to build increasingly complex applications, the need for a standardized, built-in structured logging solution became clear. However, [previous attempts](https://docs.google.com/document/d/1shW9DZJXOeGbG9Mr9Us9MiaPqmlcVatD_D8lrOXRNMU/edit#heading=h.2yd85cpp0hby) to address this issue within the Go standard library have not been successful.

The absence of a standardized structured logging solution led to the emergence of a plethora of third-party libraries, each aiming to fill the void. While these libraries have provided developers with viable options for implementing structured logging in their applications, their coexistence has also contributed to fragmentation within the Go ecosystem.

This fragmentation has led to several problems that hinder the development and maintenance of Go applications.

First and foremost, the lack of a standardized approach to structured logging has resulted in difficulties in interoperability between different Go projects. For instance, when two or more projects that use different logging libraries need to be integrated, developers often face the challenge of bridging the gap between the disparate logging solutions.

Moreover, this fragmentation has created a lack of consensus on best practices for logging within Go applications. With multiple libraries offering various features and approaches, developers are left to navigate a maze of options, often leading to inconsistencies in how logging is implemented across different projects. This lack of consistency can make it difficult for newcomers to the language to learn and adopt best practices.

Another issue arising from the fragmentation is the increased complexity of dependency management. When using multiple third-party logging libraries, developers may encounter compatibility issues, especially as these libraries evolve and introduce breaking changes. This can result in additional work to keep dependencies up-to-date and ensure that the logging functionality remains stable.

Finally, the abundance of third-party logging libraries has led to a duplication of effort among the Go community. Instead of focusing on a unified, standardized solution, developers have spent time and resources creating and maintaining various libraries that essentially attempt to solve the same problem. This can be seen as an inefficient use of the community's collective energy and expertise.

## All hail the new `slog` package

Integrating structured logging into the Go standard library offers numerous advantages and has the potential to address many of the challenges faced by the Go community thus far. The existing fragmentation within the ecosystem has led to difficulties in interoperability, a lack of consensus on best practices, increased complexity in dependency management, and a duplication of effort. By adding a structured logging package to the Go standard library, the language can take a significant step towards overcoming these issues.

One of the main benefits of having structured logging built into the standard library is the potential for improved interoperability between different Go projects. With a standardized logging solution, developers no longer need to bridge the gap between disparate third-party libraries when integrating projects. This streamlined approach can reduce the friction associated with project integration and promote more seamless collaboration within the Go community.

Additionally, incorporating structured logging into the Go standard library can foster a consensus on best practices for logging within Go applications. With a unified solution, developers will have clearer guidance on how to implement logging effectively and consistently across projects. This can make it easier for newcomers to the language to learn and adopt best practices, ultimately enhancing the overall quality of Go applications.

The inclusion of structured logging in the Go standard library also paves the way for future improvements, such as the introduction of "structured" errors. With the foundation of a standardized logging solution in place, the language can further enhance error handling by providing more structured and actionable information when logging errors. This can improve the debugging process and overall robustness of Go libraries and applications.

## Is it too good to be true? Maybe...

While incorporating structured logging into the Go standard library offers numerous benefits, it's important to consider the challenges and downsides it may pose.

One reason that previous attempts to introduce structured logging in the Go standard library have failed is the difficulty of creating a solution that caters to a wide variety of use cases. Tools included in a standard library should be as universal as possible. There have already been voices raised against the current proposal, including from prominent communities like [Kubernetes](https://github.com/golang/go/issues/56345#issuecomment-1471593372). It's crucial to acknowledge that it may be impossible to create a solution that satisfies every developer's needs, which could lead some to continue relying on third-party libraries and also raise questions about whether this feature should be included in the standard library.

Another concern with adding such a solution to the Go standard library is that it is bound by the Go backward compatibility promise, which means that any feature added to the library must be maintained and supported throughout future versions of the language. This constraint makes it difficult to address design flaws or make iterative improvements to the structured logging package once it has been added to the standard library. In contrast, community-driven efforts like [OpenTelemetry](https://opentelemetry.io/) can undergo multiple iterations before being marked as generally available and can even introduce breaking changes in major versions if necessary.

Lastly, introducing a new structured logging package in the Go standard library does not guarantee immediate adoption within the broader ecosystem. Reusable components and libraries often support multiple versions of Go for an extended period,
which means it could take considerable time before the new package becomes widely used. This delayed adoption can hinder the realization of the benefits that a standardized structured logging solution offers. Once again, a community-driven effort does not have this problem.

## Conclusion

The addition of structured logging to the Go standard library is undoubtedly a significant step towards enhancing the language and addressing existing challenges within the ecosystem. A standardized solution has the potential to improve interoperability, promote best practices, and simplify dependency management. However, it's essential to acknowledge the hurdles that must be overcome before this feature can be successfully implemented and widely adopted.

As discussed, the process of designing a universally applicable solution is challenging, and immediate adoption within the broader ecosystem is not guaranteed. It's also important to consider the constraints imposed by the Go backward compatibility promise, which can limit the ability to address design flaws.

Given these concerns, community-driven projects like OpenTelemetry may provide a more suitable home for a standard structured logging solution in Go. Community-driven projects can be more flexible and adaptable, allowing for multiple iterations and even breaking changes when necessary.
However, it's also worth recognizing that the process of adding a feature to the Go standard library often begins with experimentation and exploration within the community itself.