go-logkeycheck

command module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-logkeycheck

Lint Go programs that use the go.uber.org/zap or planetscale/log loggers to ensure log field names are consistent.

Current linting rules:

  1. Log field names should be snake_case.

Install

Install latest:

go install github.com/planetscale/go-logkeycheck@latest

Or specify a version:

go install github.com/planetscale/go-logkeycheck@v0.0.1

Refer to github releases for a list of available versions.

Example

Assume we have the following file:

package main

import (
  "github.com/planetscale/log"
  // "go.uber.org/zap" is also supported
)

func main() {
  logger := log.NewPlanetScaleLogger()
  defer logger.Sync()

  logger.Info("info log with fields",
    log.String("user_id", "12345678"),
    log.String("branchId", "abcdefghijkl"),
  )

Run go-logkeycheck to check if our log fields are in the correct format:

$ go-logkeycheck ./...
main.go:13:17: log key 'branchId' should be snake_case.

Usage

Run the linter on a file, directory, or Go package:

go-logkeycheck foo.go # lint a single file
go-logkeycheck ./...  # recursively lint all files

golangci-lint plugin support

Support for running in plugin mode is experimental.

The linter can be compiled as a plugin for golangci-lint.

You will need to compile a CGO_ENABLED=1 version of golangci-lint. The official binaries are not compatible with plugins.

Run make build-plugin to generate the plugin object go-logkeycheck.so.

Include the following in your golangci.yml file:

linters-settings:
  custom:
    logkeycheck:
      path: ./go-logkeycheck.so

linters:
  enable:
    - logkeycheck

Credits

This tool is built on top of the go/analysis package that makes it easy to write customer linters in Go.

If you are interested in writing a linter for Go check out these excellent resources which were invaluable in writing this tool:

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL