Skip to content

Commit 564bf53

Browse files
author
NimbleMike
committed
logger: Windows Notepad requires CRLF and not just LF
* Problem: * Windows Notepad, prior to Server 2019, does not display log files properly if they only end with LF * Windows programs end lines with CRLF instead of just LF * Implementation: * Modified the logger.Fire() routine to add a custom check for Windows only * The logrus Format() handler ends the line with just a LF * The additional code scans for any trailing LF (excluding trailing CRLF) and replaces it with CRLF * Testing: * Verified log file viewable under windows * Reviewers: * @shivamerla, @raunak * Bug: SV-764
1 parent 43272f9 commit 564bf53

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

logger/logger.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Hewlett Packard Enterprise Development LP
1+
// Copyright 2020 Hewlett Packard Enterprise Development LP
22

33
package logger
44

@@ -332,6 +332,17 @@ func (hook *FileHook) Fire(entry *log.Entry) error {
332332
return err
333333
}
334334

335+
// For Windows only, insert '/r' in front of any tailing '/n'. Windows text files end
336+
// lines with CRLF while other platforms just end with LF.
337+
if runtime.GOOS == "windows" {
338+
for i := len(lineBytes) - 1; i > 0; i-- {
339+
if (lineBytes[i] != '\n') || (i > 0 && lineBytes[i-1] == '\r') {
340+
break
341+
}
342+
lineBytes = append(lineBytes[:i], append([]byte{'\r'}, lineBytes[i:]...)...)
343+
}
344+
}
345+
335346
hook.logWriter.Write(lineBytes)
336347
return nil
337348
}

0 commit comments

Comments
 (0)