@@ -172,6 +172,7 @@ type Logger struct {
172172 // true: <name>.log-<timestamp>-<reason>
173173 AppendTimeAfterExt bool `json:"appendTimeAfterExt" yaml:"appendTimeAfterExt"`
174174
175+
175176 // Internal fields
176177 size int64 // current size of the log file
177178 file * os.File // current log file
@@ -646,6 +647,7 @@ func (l *Logger) openNew(reasonForBackup string) error {
646647 }
647648
648649 newname := backupName (name , l .LocalTime , reasonForBackup , rotationTimeForBackup , l .BackupTimeFormat , l .AppendTimeAfterExt )
650+
649651 if errRename := osRename (name , newname ); errRename != nil {
650652 return fmt .Errorf ("can't rename log file: %s" , errRename )
651653 }
@@ -692,6 +694,7 @@ func (l *Logger) shouldTimeRotate() bool {
692694// ("time" or "size") between the filename prefix and the extension.
693695// It uses the local time if requested (otherwise UTC).
694696func backupName (name string , local bool , reason string , t time.Time , fileTimeFormat string , appendTimeAfterExt bool ) string {
697+
695698 dir := filepath .Dir (name )
696699 filename := filepath .Base (name )
697700 ext := filepath .Ext (filename )
@@ -703,6 +706,7 @@ func backupName(name string, local bool, reason string, t time.Time, fileTimeFor
703706 }
704707 // Format the timestamp for the backup file.
705708 timestamp := t .In (currentLoc ).Format (fileTimeFormat )
709+
706710 if appendTimeAfterExt {
707711 // <name><ext>-<ts>-<reason>
708712 // e.g. httpd.log-2025-01-01T00-00-00.000-size
@@ -963,6 +967,7 @@ func (l *Logger) timeFromName(filename, prefix, ext string) (time.Time, error) {
963967 }
964968
965969 if ! l .AppendTimeAfterExt {
970+
966971 // Keep legacy behavior for error messages to satisfy existing tests
967972 if ! strings .HasPrefix (filename , prefix ) {
968973 return time.Time {}, errors .New ("mismatched prefix" )
@@ -994,6 +999,7 @@ func (l *Logger) timeFromName(filename, prefix, ext string) (time.Time, error) {
994999
9951000 // nameNoComp = "<base>-<timestamp>-<reason>"
9961001 trimmed := nameNoComp [len (base )+ 1 :]
1002+
9971003 lastHyphenIdx := strings .LastIndex (trimmed , "-" )
9981004 if lastHyphenIdx == - 1 {
9991005 return time.Time {}, fmt .Errorf ("malformed backup filename: %q" , filename )
0 commit comments