natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.79k stars 591 forks source link

How to customize the backup file name? #109

Open dequanLi opened 4 years ago

dequanLi commented 4 years ago

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))
}
alpha-baby commented 3 years ago

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

iredmail commented 3 years ago

At least make the timestamp configurable please. :)

vkazmirchuk commented 3 years ago

At least make the timestamp configurable please. :)

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

natefinch commented 3 years ago

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.

sergerdn commented 3 years ago

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.

phuslu commented 3 years ago

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

ychensha commented 3 years ago

@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.

wafaking commented 2 years ago

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.

hvisage commented 2 years ago

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