sloglint

repository·main·Indexed 42 days ago

https://github.com/go-simpler/sloglint

A Go linter designed to enforce consistent code style and best practices when using the log/slog package. It can be used as a standalone CLI tool or as a plugin for golangci-lint. Features include checks for global logger usage, context requirement, static log messages, log key naming conventions, and support for analyzing custom logging functions.

Tokens
2.7K
Snippets
14
Records
17
Agent score
55%

What's inside sloglint

  1. Install sloglint via golangci-lint

    main

    The recommended way to use sloglint is as a plugin for golangci-lint. To enable it, add sloglint to the enable list in your .golangci.yaml configuration file.

    # .golangci.yaml
    linters:
      enable:
        - sloglint
  2. Configure static log message checks

    main

    Enable static-msg to report dynamic log messages (e.g., those built with fmt.Sprintf) and ensure messages are string literals or constants.

    # .golangci.yaml
    linters:
      settings:
        sloglint:
          static-msg: true
  3. Configure log message style

    main

    Use msg-style to enforce a specific casing for log messages. Supported styles are:

    • lowercased: The first letter must be lowercase.
    • capitalized: The first letter must be uppercase.
    # .golangci.yaml
    linters:
      settings:
        sloglint:
          msg-style: "lowercased" # Or "capitalized".
  4. Configure log argument constraints

    main

    You can enforce specific patterns for log arguments using the following settings:

    • kv-only: true: Reports use of attributes; suggests using ...any functions instead of ...Attr alternatives.
    • attr-only: true: Reports use of key-value pairs; suggests using ...Attr functions instead of ...any alternatives.
    • args-on-sep-lines: true: Reports when two or more arguments are on the same line.
    # .golangci.yaml
    linters:
      settings:
        sloglint:
          kv-only: true
          attr-only: true
          args-on-sep-lines: true
  5. Configure log key constraints

    main

    Control how log keys are defined and named using these settings:

    • no-raw-keys: true: Reports the use of string literals as keys (requires constants).
    • allowed-keys: A list of explicitly allowed keys.
    • forbidden-keys: A list of keys that are prohibited (e.g., time, level, msg, source).
    • key-naming-case: Enforces a naming convention. Supported values: snake, kebab, camel, pascal.
    # .golangci.yaml
    linters:
      settings:
        sloglint:
          no-raw-keys: true
          allowed-keys:
            - user_id
          forbidden-keys:
            - time
            - level
          key-naming-case: "snake"
  6. Configure context requirement checks

    main

    Use the context setting to report log/slog functions that are called without a context.Context.

    • all: Report all functions without context.
    • scope: Report functions without context only if a context exists within the scope of the outermost function.
    # .golangci.yaml
    linters:
      settings:
        sloglint:
          context: "all" # Or "scope".
  7. Configure global logger checks

    main

    Use the no-global setting to report the use of global loggers. You can choose to report all global logger usage or only the use of slog.Default().

    # .golangci.yaml
    linters:
      settings:
        sloglint:
          no-global: "all" # Or "default".
  8. Analyze custom logging functions

    main

    You can extend sloglint to analyze custom functions by defining them in custom-funcs. For each function, you must provide:

    1. name: The full package-qualified name. For methods, wrap the receiver in parentheses (e.g., (*package.Type).Method).
    2. msg-pos: The 0-indexed position of the msg string argument. Use -1 if there is no message.
    3. args-pos: The 0-indexed position of the args ...any argument. Use -1 if there are no arguments.
    # .golangci.yaml
    linters:
      settings:
        sloglint:
          custom-funcs:
            - name: "(*golang.org/x/exp/slog.Logger).InfoContext"
              msg-pos: 1
              args-pos: 2
  9. Configure sloglint via the Options struct

    main

    The Options struct allows you to customize the behavior of the sloglint analyzer. You can control how global loggers, context usage, message styles, argument types, and log keys are validated.

    Key Configuration Categories

    Logger & Context Constraints

    • NoGlobalLogger: Set to `
  10. Initialize the sloglint analyzer with New()

    main

    The New function creates a new *analysis.Analyzer instance for use with the Go analysis framework (e.g., via golangci-lint or go vet).

    If you pass nil as the opts argument, the analyzer defaults to enabling NoMixedArguments: true. You can provide a pointer to an Options struct to configure specific linting rules such as key naming conventions, allowed/forbidden keys, and message styles.

    import "github.com/go-simpler/go-simpler/sloglint"
    
    // Create an analyzer with custom options
    opts := &sloglint.Options{
        NoMixedArguments: true,
        // ... other options
    }
    analyzer := sloglint.New(opts)