Skip to content

Commit

Permalink
#969 + #207: initiate logger but without file transports unless speci…
Browse files Browse the repository at this point in the history
…fic methods were called
  • Loading branch information
JoernBerkefeld committed Jun 6, 2023
1 parent 8cf0757 commit e5c2295
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 85 deletions.
51 changes: 32 additions & 19 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ Provides default functionality that can be overwritten by child metadata type cl
<dt><a href="#setupSDK">setupSDK(sessionKey, authObject)</a> ⇒ <code><a href="#SDK">SDK</a></code></dt>
<dd><p>Returns an SDK instance to be used for API calls</p>
</dd>
<dt><a href="#_createNewLoggerTransport">_createNewLoggerTransport([noLogFile])</a> ⇒ <code>object</code></dt>
<dd><p>wrapper around our standard winston logging to console and logfile</p>
</dd>
</dl>

## Typedefs
Expand Down Expand Up @@ -5895,7 +5892,8 @@ CLI entry for SFMC DevTools
* [._isValidType(selectedType, [handleOutside])](#Util._isValidType) ⇒ <code>boolean</code>
* [.getTypeAndSubType(selectedType)](#Util.getTypeAndSubType) ⇒ <code>Array.&lt;string&gt;</code>
* [.getRetrieveTypeChoices()](#Util.getRetrieveTypeChoices) ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
* [.startLogger([restart])](#Util.startLogger) ⇒ <code>void</code>
* [._createNewLoggerTransport([noLogFile])](#Util._createNewLoggerTransport) ⇒ <code>object</code>
* [.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>
* [.inverseGet(objs, val)](#Util.inverseGet) ⇒ <code>string</code>
Expand Down Expand Up @@ -6047,16 +6045,29 @@ helper for getDefaultProperties()

**Kind**: static method of [<code>Util</code>](#Util)
**Returns**: <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code> - type choices
<a name="Util._createNewLoggerTransport"></a>

### Util.\_createNewLoggerTransport([noLogFile]) ⇒ <code>object</code>
wrapper around our standard winston logging to console and logfile

**Kind**: static method of [<code>Util</code>](#Util)
**Returns**: <code>object</code> - initiated logger for console and file

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [noLogFile] | <code>boolean</code> | <code>false</code> | optional flag to indicate if we should log to file; CLI logs are always on |

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

### Util.startLogger([restart]) ⇒ <code>void</code>
### Util.startLogger([restart], [noLogFile]) ⇒ <code>void</code>
initiate winston logger

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

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [restart] | <code>boolean</code> | <code>false</code> | if true, logger will be restarted; otherwise, an existing logger will be used |
| [noLogFile] | <code>boolean</code> | <code>false</code> | if false, logger will log to file; otherwise, only to console |

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

Expand Down Expand Up @@ -7771,7 +7782,8 @@ Util that contains logger and simple util methods
* [._isValidType(selectedType, [handleOutside])](#Util._isValidType) ⇒ <code>boolean</code>
* [.getTypeAndSubType(selectedType)](#Util.getTypeAndSubType) ⇒ <code>Array.&lt;string&gt;</code>
* [.getRetrieveTypeChoices()](#Util.getRetrieveTypeChoices) ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
* [.startLogger([restart])](#Util.startLogger) ⇒ <code>void</code>
* [._createNewLoggerTransport([noLogFile])](#Util._createNewLoggerTransport) ⇒ <code>object</code>
* [.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>
* [.inverseGet(objs, val)](#Util.inverseGet) ⇒ <code>string</code>
Expand Down Expand Up @@ -7923,16 +7935,29 @@ helper for getDefaultProperties()

**Kind**: static method of [<code>Util</code>](#Util)
**Returns**: <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code> - type choices
<a name="Util._createNewLoggerTransport"></a>

### Util.\_createNewLoggerTransport([noLogFile]) ⇒ <code>object</code>
wrapper around our standard winston logging to console and logfile

**Kind**: static method of [<code>Util</code>](#Util)
**Returns**: <code>object</code> - initiated logger for console and file

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [noLogFile] | <code>boolean</code> | <code>false</code> | optional flag to indicate if we should log to file; CLI logs are always on |

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

### Util.startLogger([restart]) ⇒ <code>void</code>
### Util.startLogger([restart], [noLogFile]) ⇒ <code>void</code>
initiate winston logger

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

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [restart] | <code>boolean</code> | <code>false</code> | if true, logger will be restarted; otherwise, an existing logger will be used |
| [noLogFile] | <code>boolean</code> | <code>false</code> | if false, logger will log to file; otherwise, only to console |

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

Expand Down Expand Up @@ -8192,18 +8217,6 @@ Returns an SDK instance to be used for API calls
| sessionKey | <code>string</code> | key for specific BU |
| authObject | <code>TYPE.AuthObject</code> | credentials for specific BU |

<a name="_createNewLoggerTransport"></a>

## \_createNewLoggerTransport([noLogFile]) ⇒ <code>object</code>
wrapper around our standard winston logging to console and logfile

**Kind**: global function
**Returns**: <code>object</code> - initiated logger for console and file

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [noLogFile] | <code>boolean</code> | <code>false</code> | optional flag to indicate if we should log to file; CLI logs are always on |

<a name="TypeKeyCombo"></a>

## TypeKeyCombo : <code>Object.&lt;string, string&gt;</code>
Expand Down
147 changes: 81 additions & 66 deletions lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,65 @@ const Util = {

return typeChoices;
},
/**
* wrapper around our standard winston logging to console and logfile
*
* @param {boolean} [noLogFile=false] optional flag to indicate if we should log to file; CLI logs are always on
* @returns {object} initiated logger for console and file
*/
_createNewLoggerTransport: function (noLogFile = false) {
// {
// error: 0,
// warn: 1,
// info: 2,
// http: 3,
// verbose: 4,
// debug: 5,
// silly: 6
// }
const logFileName = new Date().toISOString().split(':').join('.');
const transports = {
console: new winston.transports.Console({
// Write logs to Console
level: Util.OPTIONS.loggerLevel || 'info',
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp({ format: 'HH:mm:ss' }),
winston.format.simple(),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`
)
),
}),
};
if (!noLogFile) {
transports.file = new winston.transports.File({
// Write logs to logfile
filename: 'logs/' + logFileName + '.log',
level: 'debug', // log everything
format: winston.format.combine(
winston.format.timestamp({ format: 'HH:mm:ss.SSS' }),
winston.format.simple(),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`
)
),
});
transports.fileError = new winston.transports.File({
// Write logs to additional error-logfile for better visibility of errors
filename: 'logs/' + logFileName + '-errors.log',
level: 'error', // only log errors
format: winston.format.combine(
winston.format.timestamp({ format: 'HH:mm:ss.SSS' }),
winston.format.simple(),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`
)
),
});
}
return transports;
},

loggerTransports: null,
/**
Expand All @@ -238,15 +297,25 @@ const Util = {
* initiate winston logger
*
* @param {boolean} [restart=false] if true, logger will be restarted; otherwise, an existing logger will be used
* @param {boolean} [noLogFile=false] if false, logger will log to file; otherwise, only to console
* @returns {void}
*/
startLogger: function startLogger(restart = false) {
if (Util.loggerTransports !== null && !restart) {
startLogger: function (restart = false, noLogFile = false) {
if (
!(
Util.loggerTransports === null ||
restart ||
(!Util.loggerTransports?.file && !noLogFile && !Util.OPTIONS?.noLogFile)
)
) {
// logger already started
return;
}
Util.loggerTransports = _createNewLoggerTransport(Util.OPTIONS.noLogFile);
Util.loggerTransports = this._createNewLoggerTransport(
noLogFile || Util.OPTIONS?.noLogFile
);
const myWinston = winston.createLogger({
level: Util.OPTIONS.loggerLevel,
levels: winston.config.npm.levels,
transports: Object.values(Util.loggerTransports),
});
Expand Down Expand Up @@ -544,34 +613,30 @@ const Util = {
* @returns {void}
*/
setLoggingLevel(argv) {
Util.loggerTransports.console.file = 'debug';
if (argv.silent) {
// only errors printed to CLI
Util.logger.level = 'error';
Util.OPTIONS.loggerLevel = 'error';
Util.loggerTransports.console.level = 'error';
Util.logger.debug('CLI logger set to: silent');
} else if (argv.verbose) {
// chatty user cli logs
Util.logger.level = 'verbose';
Util.OPTIONS.loggerLevel = 'verbose';
Util.loggerTransports.console.level = 'verbose';
Util.logger.debug('CLI logger set to: verbose');
} else {
// default user cli logs
// TODO to be switched to "warn" when cli-process is integrated
Util.logger.level = 'info';
Util.OPTIONS.loggerLevel = 'info';
Util.loggerTransports.console.level = 'info';
Util.logger.debug('CLI logger set to: info / default');
}
if (argv.debug) {
// enables developer output & features. no change to actual logs
Util.logger.level = 'debug';
Util.OPTIONS.loggerLevel = 'debug';
Util.loggerTransports.console.level = 'debug';
Util.logger.debug('CLI logger set to: debug');
}
if (Util.loggerTransports?.console) {
Util.loggerTransports.console.level = Util.OPTIONS.loggerLevel;
}
if (Util.logger) {
Util.logger.level = Util.OPTIONS.loggerLevel;
}
},
/**
* outputs a warning that the given type is still in beta
Expand Down Expand Up @@ -716,57 +781,7 @@ const Util = {
return null;
},
};
/**
* wrapper around our standard winston logging to console and logfile
*
* @param {boolean} [noLogFile=false] optional flag to indicate if we should log to file; CLI logs are always on
* @returns {object} initiated logger for console and file
*/
function _createNewLoggerTransport(noLogFile = false) {
// {
// error: 0,
// warn: 1,
// info: 2,
// http: 3,
// verbose: 4,
// debug: 5,
// silly: 6
// }
const logFileName = new Date().toISOString().split(':').join('.');
const transports = {
console: new winston.transports.Console({
// Write logs to Console
level: 'info', // log error, warn, info
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp({ format: 'HH:mm:ss' }),
winston.format.simple(),
winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)
),
}),
};
if (!noLogFile) {
transports.file = new winston.transports.File({
// Write logs to logfile
filename: 'logs/' + logFileName + '.log',
level: 'debug', // log everything
format: winston.format.combine(
winston.format.timestamp({ format: 'HH:mm:ss.SSS' }),
winston.format.simple(),
winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)
),
});
transports.fileError = new winston.transports.File({
// Write logs to additional error-logfile for better visibility of errors
filename: 'logs/' + logFileName + '-errors.log',
level: 'error', // only log errors
format: winston.format.combine(
winston.format.timestamp({ format: 'HH:mm:ss.SSS' }),
winston.format.simple(),
winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)
),
});
}
return transports;
}

Util.startLogger(false, true);

module.exports = Util;

0 comments on commit e5c2295

Please sign in to comment.