https://github.com/nicklasfrahm-dev/appkit
A set of useful libraries to quickly build a Go application.
https://github.com/nicklasfrahm-dev/appkit
go library
Last synced: over 1 year ago
JSON representation
A set of useful libraries to quickly build a Go application.
- Host: GitHub
- URL: https://github.com/nicklasfrahm-dev/appkit
- Owner: nicklasfrahm-dev
- License: mit
- Created: 2024-09-28T20:31:38.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-07T14:17:34.000Z (over 1 year ago)
- Last Synced: 2025-02-02T03:18:00.946Z (over 1 year ago)
- Topics: go, library
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# App Kit
This repository provides a set of useful libraries that can be used to quickly build a Go applications.
```shell
go get github.com/nicklasfrahm-dev/appkit
```
## Logging
This package provides a simple logging library based on [zap][github-zap], which may be configured at runtime using the environment variables shown below. The logger is optimized for structured logging using [Loki][github-loki] as a log aggregation system. By default the logger will use a production-ready configuration, but it can be configured to output logs in a more human-readable format for development purposes.
| Environment variable | Description | Default | Allowed values |
| -------------------- | -------------------------------- | ------- | ----------------------------------------- |
| `LOG_LEVEL` | The minimum log level to output. | `info` | `debug`, `info`, `warn`, `error`, `fatal` |
| `LOG_FORMAT` | The log format to use. | `json` | `json`, `console` |
### Example
```go
package main
import (
"context"
"fmt"
"github.com/nicklasfrahm-dev/appkit/logging"
"go.uber.org/zap"
)
func main() {
logger := logging.NewLogger()
port := 8080
// Don't do this.
logger.Info(fmt.Sprintf("Starting HTTP server on port %d", port))
logger.Sugar().Infof("Starting HTTP server on port %d", port)
// Do this instead.
logger.Info("Starting HTTP server", zap.Int("port", port))
printLog(logger)
// This is how you can use a context to pass a logger around.
ctx := logging.WithLogger(context.Background(), logger)
printLogWithContext(ctx)
// This is how you can add fields to the logger in the context.
printLogWithContext(logging.WithFields(ctx, zap.String("key", "value")))
}
// This is how you can pass the logger around.
func printLog(logger *zap.Logger) {
logger.Info("This is a log message", zap.String("key", "value"))
}
// This is how you can use a context to pass the logger around.
func printLogWithContext(ctx context.Context) {
logger := logging.FromContext(ctx)
logger.Info("This is a log message", zap.String("key", "value"))
}
```
## License
This project is and will always be licensed under the MIT License. See the [LICENSE.md](LICENSE.md) file for more information.
[github-zap]: https://github.com/uber-go/zap
[github-loki]: https://github.com/grafana/loki