uber-go/zap · error

no encoder name specified

Error message

no encoder name specified

What it means

RegisterEncoder (e.g. LowercaseColorLevelEncoder / CapitalColorLevelEncoder wrappers) requires a non-empty name under which the encoder constructor is stored. Passing an empty name returns the sentinel errNoEncoderNameSpecified because there would be no way to look the encoder up later.

Source

Thrown at encoder.go:32

// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zap

import (
	"errors"
	"fmt"
	"sync"

	"go.uber.org/zap/zapcore"
)

var (
	errNoEncoderNameSpecified = errors.New("no encoder name specified")

	_encoderNameToConstructor = map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error){
		"console": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
			return zapcore.NewConsoleEncoder(encoderConfig), nil
		},
		"json": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
			return zapcore.NewJSONEncoder(encoderConfig), nil
		},
	}
	_encoderMutex sync.RWMutex
)

// RegisterEncoder registers an encoder constructor, which the Config struct
// can then reference. By default, the "json" and "console" encoders are
// registered.
//
// Attempting to register an encoder whose name is already taken returns an
// error.

View on GitHub (pinned to bbd4ecbd87)

Solutions

  1. Pass a non-empty name string to RegisterEncoder, e.g. zap.RegisterEncoder("myjson", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error){...})
  2. Validate/guard the encoder name variable before registering

Example fix

// before
err := zap.RegisterEncoder(name, ctor) // name == ""
// after
if name == "" { return errors.New("encoder name required") }
err := zap.RegisterEncoder(name, ctor)
Defensive patterns

Strategy: validation

Validate before calling

if name == "" {
    return fmt.Errorf("encoder name required")
}
err := zap.RegisterEncoder(name, ctor)

Try / catch

if err := zap.RegisterEncoder(name, ctor); err != nil {
    return fmt.Errorf("register encoder: %w", err)
}

Prevention

When it happens

Trigger: Calling zap.RegisterEncoder("", constructor) or the color-level encoder registration helpers with an empty encoder name string.

Common situations: Programmatic registration where the name comes from a variable/config value that is empty; test code building encoders dynamically without validating the name first.

Related errors


AI-assisted analysis of uber-go/zap@bbd4ecbd87 (2026-08-31). Data as JSON: /api/errors/40f136bc447f6712. Report an issue: GitHub.