lumberjack icon indicating copy to clipboard operation
lumberjack copied to clipboard

How to customize the backup file name?

Open dequanLi opened this issue 5 years ago • 9 comments

Now,the backup file name is generated by the following function,the format only be “name-timestamp-ext”, but i need other format,Such as “name-ext-timestamp”.How should i do?

func backupName(name string, local bool) string {
	dir := filepath.Dir(name)
	filename := filepath.Base(name)
	ext := filepath.Ext(filename)
	prefix := filename[:len(filename)-len(ext)]
	t := currentTime()
	if !local {
		t = t.UTC()
	}

	timestamp := t.Format(backupTimeFormat)
	return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext))
}

dequanLi avatar Sep 07 '20 11:09 dequanLi

Now,the backup file name is generated by the following function,the format only be “name-timestamp-ext”, but i need other format,Such as “name-ext-timestamp”.How should i do?

func backupName(name string, local bool) string {
	dir := filepath.Dir(name)
	filename := filepath.Base(name)
	ext := filepath.Ext(filename)
	prefix := filename[:len(filename)-len(ext)]
	t := currentTime()
	if !local {
		t = t.UTC()
	}

	timestamp := t.Format(backupTimeFormat)
	return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext))
}

me too

alpha-baby avatar Sep 14 '20 11:09 alpha-baby

At least make the timestamp configurable please. :)

iredmail avatar Nov 10 '20 14:11 iredmail

At least make the timestamp configurable please. :)

See https://github.com/natefinch/lumberjack/pull/118

vkazmirchuk avatar Nov 13 '20 08:11 vkazmirchuk

The problem with making it configurable is that it is too easy to screw up in non-obvious ways. The timestamp is what makes the backup files unique, and it is how lumberjack figures out when a file was created (granted this last part could just use the os timestamp).

Really, there's no reason why anyone should care what the timestamp looks like. It's just a backup file.

natefinch avatar Nov 13 '20 12:11 natefinch

Hi, @natefinch. Thanks for reply.

The problem with making it configurable is that it is too easy to screw up in non-obvious ways. The timestamp is what makes the backup files unique, and it is how lumberjack figures out when a file was created (granted this last part could just use the os timestamp).

Yeah, you are right. Some things might be confused for some developers. But I'm pretty sure that every developer must understand what his doing.

Really, there's no reason why anyone should care what the timestamp looks like. It's just a backup file.

In my case I have a script that pushes archived logs to my own storage data. On my side it is not just backup file and I really need make name of archive file is configurable. Because I have many logs from many services and I want to have one format for logs and archived file.

sergerdn avatar Nov 15 '20 02:11 sergerdn

If TimeFormat was added finally, the rotate logic should base on file ture modified time nor time field of filename. https://github.com/natefinch/lumberjack/blob/v2.0/lumberjack.go#L400

Because if user change time format in a new application version (e.g. "2020-11-27" to "Nov-11-2020"), the rotate will be buggy. my phuslu/log was reported by this issue and fixed it just now

phuslu avatar Nov 27 '20 02:11 phuslu

@natefinch For collecting the current log to a remote storage, we want the current and backups have different ext. Currently, turn on compress is the only way, so customization is wanted.

ychensha avatar Jul 04 '21 09:07 ychensha

Now,the backup file name is generated by the following function,the format only be “name-timestamp-ext”, but i need other format,Such as “name-ext-timestamp”.How should i do?

func backupName(name string, local bool) string {
	dir := filepath.Dir(name)
	filename := filepath.Base(name)
	ext := filepath.Ext(filename)
	prefix := filename[:len(filename)-len(ext)]
	t := currentTime()
	if !local {
		t = t.UTC()
	}

	timestamp := t.Format(backupTimeFormat)
	return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext))
}

i also has the requierment for self define the bakcup file name.

wafaking avatar Jul 13 '22 02:07 wafaking

if the TZ was just aded to the BACK, and not the ext moved, it would've solved other problems.

hvisage avatar Sep 08 '22 05:09 hvisage