Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid compressed data--format violated #1392

Open
wuyinq opened this issue Dec 5, 2023 · 4 comments
Open

invalid compressed data--format violated #1392

wuyinq opened this issue Dec 5, 2023 · 4 comments

Comments

@wuyinq
Copy link

wuyinq commented Dec 5, 2023

Describe the bug
when I use 'zap' to manager my service log, i find sometimes the **.log.gz can't be unzip using 'gunzip' command.

I get this when i run "gunzip ***.log.gz":

  1. gzip: ***.log.gz: invalid compressed data--format violated
  2. gzip: ***.log.gz: not in gzip format
@sywhang
Copy link
Contributor

sywhang commented Dec 5, 2023

Can you please clarify your setup a little bit? Zap just writes to a file without compression by default. If you have specific configuration that syncs as a gzip formatted file, maybe that custom zapcore implementation might have issues; But zap by default does not write its output as a gzip'd file.

@wuyinq
Copy link
Author

wuyinq commented Dec 6, 2023

Can you please clarify your setup a little bit? Zap just writes to a file without compression by default. If you have specific configuration that syncs as a gzip formatted file, maybe that custom zapcore implementation might have issues; But zap by default does not write its output as a gzip'd file.

here

func initLogger(filePath string, atomicLevel *zap.AtomicLevel, fields ...zap.Field) (*FMLogger, error) {
	// 创建日志配置
	var encoderConfig = zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

	// 创建Console输出Core
	consoleEncoder := zapcore.NewJSONEncoder(encoderConfig)
	consoleCore := zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), atomicLevel)

	// 创建日志输出Core
	fmt.Printf("Init logger setting: MaxSize: %d, MaxAge: %d, MaxBackups: %d, logPath: %s\n", 
		config.GlobalConfig.LogRotateSize, config.GlobalConfig.LogMaxAge, config.GlobalConfig.LogBackupCount, filePath)
	fileWriter := zapcore.AddSync(&lumberjack.Logger{
		Filename:   filePath,
		MaxSize:    config.GlobalConfig.LogRotateSize, 	// megabytes
		MaxAge: 	config.GlobalConfig.LogMaxAge, 		// backup log files 7 Days
		MaxBackups: config.GlobalConfig.LogBackupCount,	// backup log files 100
		Compress:   true,
	})
	fileCore := zapcore.NewCore(
		zapcore.NewJSONEncoder(encoderConfig),
		fileWriter,
		atomicLevel,
	)

	cores := zapcore.NewTee(fileCore, consoleCore)
	log := zap.New(cores)
	log = log.WithOptions(
		zap.ErrorOutput(os.Stdout), // error message output to stdout
		zap.AddCaller(),            // add function caller info to log
		zap.AddCallerSkip(1),       // make stack having right depth to get function call
		zap.Fields(fields...),      // add common log info, like local_ip
	)

	logger := FMLogger{
		logger: log.Sugar(),
		ctx:    context.Background(),
	}

	return &logger, nil
}

Maybe because of ' lumberjack'? I'm not sure that.

fileWriter := zapcore.AddSync(&lumberjack.Logger{
		Filename:   filePath,
		MaxSize:    config.GlobalConfig.LogRotateSize, 	// megabytes
		MaxAge: 	config.GlobalConfig.LogMaxAge, 		// backup log files 7 Days
		MaxBackups: config.GlobalConfig.LogBackupCount,	// backup log files 100
		Compress:   true,
	})

@JacobOaks
Copy link
Contributor

Hi @wuyinq, it seems like the compressed files you're referring to are coming from a lumberjack Logger. Zap doesn't have any control over what lumberjack is writing to disk. I'd suggest asking the maintainers of lumberjack.

@wuyinq
Copy link
Author

wuyinq commented Dec 13, 2023

yes, I'm waiting for their reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants