Skip to content

Commit

Permalink
#1045: remove empty error log files during log rotation
Browse files Browse the repository at this point in the history
when commands are executed on multiple BUs using the * sign, logs are rotated before switching BUs
  • Loading branch information
JoernBerkefeld committed Jul 25, 2023
1 parent 27b3134 commit d705672
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
24 changes: 24 additions & 0 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6044,6 +6044,7 @@ CLI entry for SFMC DevTools
* [.getTypeAndSubType(selectedType)](#Util.getTypeAndSubType) ⇒ <code>Array.&lt;string&gt;</code>
* [.getRetrieveTypeChoices()](#Util.getRetrieveTypeChoices) ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
* [._createNewLoggerTransport([noLogFile])](#Util._createNewLoggerTransport) ⇒ <code>object</code>
* [.deleteEmptyLogFile([noLogFile])](#Util.deleteEmptyLogFile)
* [.startLogger([restart], [noLogFile])](#Util.startLogger) ⇒ <code>void</code>
* [.metadataLogger(level, type, method, payload, [source])](#Util.metadataLogger) ⇒ <code>void</code>
* [.replaceByObject(str, obj)](#Util.replaceByObject) ⇒ <code>string</code> \| <code>object</code>
Expand Down Expand Up @@ -6212,6 +6213,17 @@ wrapper around our standard winston logging to console and logfile
| --- | --- | --- |
| [noLogFile] | <code>boolean</code> | optional flag to indicate if we should log to file; CLI logs are always on |

<a name="Util.deleteEmptyLogFile"></a>

### Util.deleteEmptyLogFile([noLogFile])
helper for [startLogger](#Util.startLogger)

**Kind**: static method of [<code>Util</code>](#Util)

| Param | Type | Description |
| --- | --- | --- |
| [noLogFile] | <code>boolean</code> | if false, logger will log to file; otherwise, only to console |

<a name="Util.startLogger"></a>

### Util.startLogger([restart], [noLogFile]) ⇒ <code>void</code>
Expand Down Expand Up @@ -7988,6 +8000,7 @@ Util that contains logger and simple util methods
* [.getTypeAndSubType(selectedType)](#Util.getTypeAndSubType) ⇒ <code>Array.&lt;string&gt;</code>
* [.getRetrieveTypeChoices()](#Util.getRetrieveTypeChoices) ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
* [._createNewLoggerTransport([noLogFile])](#Util._createNewLoggerTransport) ⇒ <code>object</code>
* [.deleteEmptyLogFile([noLogFile])](#Util.deleteEmptyLogFile)
* [.startLogger([restart], [noLogFile])](#Util.startLogger) ⇒ <code>void</code>
* [.metadataLogger(level, type, method, payload, [source])](#Util.metadataLogger) ⇒ <code>void</code>
* [.replaceByObject(str, obj)](#Util.replaceByObject) ⇒ <code>string</code> \| <code>object</code>
Expand Down Expand Up @@ -8156,6 +8169,17 @@ wrapper around our standard winston logging to console and logfile
| --- | --- | --- |
| [noLogFile] | <code>boolean</code> | optional flag to indicate if we should log to file; CLI logs are always on |

<a name="Util.deleteEmptyLogFile"></a>

### Util.deleteEmptyLogFile([noLogFile])
helper for [startLogger](#Util.startLogger)

**Kind**: static method of [<code>Util</code>](#Util)

| Param | Type | Description |
| --- | --- | --- |
| [noLogFile] | <code>boolean</code> | if false, logger will log to file; otherwise, only to console |

<a name="Util.startLogger"></a>

### Util.startLogger([restart], [noLogFile]) ⇒ <code>void</code>
Expand Down
44 changes: 27 additions & 17 deletions lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,30 @@ const Util = {
* @type {TYPE.Logger}
*/
logger: null,
/**
* helper for {@link Util.startLogger}
*
* @param {boolean} [noLogFile] if false, logger will log to file; otherwise, only to console
*/
deleteEmptyLogFile: function (noLogFile) {
// * this is a workaround for as long as winston logger cannot be told to only create the error log file if there was an error
if (
!Util.loggedErrors &&
Util.loggerTransports?.fileError &&
!noLogFile &&
!Util.OPTIONS?.noLogFile
) {
try {
fs.unlinkSync(
Util.loggerTransports?.fileError.filename.startsWith('logs/')
? Util.loggerTransports?.fileError.filename
: 'logs/' + Util.loggerTransports?.fileError.filename
);
} catch {
// fail silently
}
}
},
/**
* initiate winston logger
*
Expand All @@ -308,6 +332,8 @@ const Util = {
* @returns {void}
*/
startLogger: function (restart = false, noLogFile = false) {
Util.deleteEmptyLogFile(noLogFile);
Util.loggedErrors = false;
if (
!(
Util.loggerTransports === null ||
Expand All @@ -328,23 +354,7 @@ const Util = {
});

process.on('exit', () => {
// * this is a workaround for as long as winston logger cannot be told to only create the error log file if there was an error
if (
!Util.loggedErrors &&
Util.loggerTransports?.fileError &&
!noLogFile &&
!Util.OPTIONS?.noLogFile
) {
try {
fs.unlinkSync(
Util.loggerTransports?.fileError.filename.startsWith('logs/')
? Util.loggerTransports?.fileError.filename
: 'logs/' + Util.loggerTransports?.fileError.filename
);
} catch {
// fail silently
}
}
Util.deleteEmptyLogFile(noLogFile);
});

const winstonError = myWinston.error;
Expand Down

0 comments on commit d705672

Please sign in to comment.