Skip to content

Question: WIndows - How to specify full path to log file name #994

@stuartstein777

Description

@stuartstein777

I have this:

func getLogger() (*zap.Logger, error) {
	dir, err := os.Executable()
	if err != nil {
		panic(err)
	}
	fullpath := filepath.Join(filepath.Dir(dir), "proxy.log")
	cfg := zap.NewProductionConfig()
	cfg.OutputPaths = []string{
		fullpath,
	}
	return cfg.Build()
}

fullpath here is C:\Users\stuarts\Source\sentilan-agent-proxy-api\proxy.log

When I try to log I get a nil dereference error. The logger that is returned is nil.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff65d95a787]

However, this works.

func getLogger() (*zap.Logger, error) {
	cfg := zap.NewProductionConfig()
	cfg.OutputPaths = []string{
		"proxy.log",
	}
	return cfg.Build()
}

This logs to proxy.log in the same folder as the .exe, But we are running this s a service and its causing the file to be logged to windows system folder (as this is the working folder when its run a service).

What am I doing wrong here? I'd like to specify a full path to log file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions