@@ -31,2 +31,4 @@ 'use strict'; | ||
| var logLevels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']; | ||
| var createLogger = function createLogger(onMessage /*: OnMessageEventHandlerType*/) { | ||
@@ -65,2 +67,35 @@ var parentContext /*: MessageContextType*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| var _loop = function _loop(logLevel) { | ||
| log[logLevel] = function (a, b, c, d, e, f, g, h, i, k) { | ||
| return log.child({ | ||
| logLevel: logLevel | ||
| })(a, b, c, d, e, f, g, h, i, k); | ||
| }; | ||
| }; | ||
| var _iteratorNormalCompletion = true; | ||
| var _didIteratorError = false; | ||
| var _iteratorError = undefined; | ||
| try { | ||
| for (var _iterator = logLevels[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
| var logLevel = _step.value; | ||
| _loop(logLevel); | ||
| } | ||
| } catch (err) { | ||
| _didIteratorError = true; | ||
| _iteratorError = err; | ||
| } finally { | ||
| try { | ||
| if (!_iteratorNormalCompletion && _iterator.return) { | ||
| _iterator.return(); | ||
| } | ||
| } finally { | ||
| if (_didIteratorError) { | ||
| throw _iteratorError; | ||
| } | ||
| } | ||
| } | ||
| return log; | ||
@@ -67,0 +102,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../../src/factories/createLogger.js"],"names":["global","ROARR","version","createLogger","onMessage","parentContext","log","a","b","c","d","e","f","g","h","i","k","time","Date","now","sequence","context","message","prepend","child"],"mappings":";;;;;;kQAAA;;AAEA;;AAQA;;;;;;;;;;;;;AAEAA,OAAOC,KAAP,GAAeD,OAAOC,KAAP,IAAgB,gDAA/B;;;;;AAIA,IAAMC,UAAU,OAAhB;;AAEA,IAAMC,eAAe,SAAfA,YAAe,CAACC,SAAD,kCAAkF;AAAA,MAA3CC,aAA2C,gGAAP,EAAO;;AACrG;AACA,MAAMC,uBAAkB,SAAlBA,GAAkB,CAACC,CAAD,EAAIC,CAAJ,EAAOC,CAAP,EAAUC,CAAV,EAAaC,CAAb,EAAgBC,CAAhB,EAAmBC,CAAnB,EAAsBC,CAAtB,EAAyBC,CAAzB,EAA4BC,CAA5B,EAAkC;AACxD,QAAMC,OAAOC,KAAKC,GAAL,EAAb;AACA,QAAMC,WAAWpB,OAAOC,KAAP,CAAamB,QAAb,EAAjB;;AAEA,QAAIC,gBAAJ;AACA,QAAIC,gBAAJ;;AAEA,QAAI,OAAOf,CAAP,KAAa,QAAjB,EAA2B;AACzBc,6BACKrB,OAAOC,KAAP,CAAasB,OADlB,EAEKlB,aAFL;AAIAiB,gBAAU,wBAAQf,CAAR,EAAWC,CAAX,EAAcC,CAAd,EAAiBC,CAAjB,EAAoBC,CAApB,EAAuBC,CAAvB,EAA0BC,CAA1B,EAA6BC,CAA7B,EAAgCC,CAAhC,EAAmCC,CAAnC,CAAV;AACD,KAND,MAMO;AACLK,6BACKrB,OAAOC,KAAP,CAAasB,OADlB,EAEKlB,aAFL,EAGKE,CAHL;;AAMAe,gBAAU,wBAAQd,CAAR,EAAWC,CAAX,EAAcC,CAAd,EAAiBC,CAAjB,EAAoBC,CAApB,EAAuBC,CAAvB,EAA0BC,CAA1B,EAA6BC,CAA7B,EAAgCC,CAAhC,CAAV;AACD;;AAEDZ,cAAU;AACRiB,sBADQ;AAERC,sBAFQ;AAGRF,wBAHQ;AAIRH,gBAJQ;AAKRf;AALQ,KAAV;AAOD,GA9BD;;AAgCAI,MAAIkB,KAAJ,GAAY,UAACH,OAAD,2BAAiC;AAC3C,WAAOlB,aAAaC,SAAb,eACFC,aADE,EAEFgB,OAFE,EAAP;AAID,GALD;;AAOA,SAAOf,GAAP;AACD,CA1CD;;kBA4CeH,Y","file":"createLogger.js","sourcesContent":["// @flow\n\nimport {\n sprintf\n} from 'sprintf-js';\nimport type {\n LoggerType,\n MessageContextType,\n MessageType\n} from '../types';\nimport createRoarrInititialGlobalState from './createRoarrInititialGlobalState';\n\nglobal.ROARR = global.ROARR || createRoarrInititialGlobalState();\n\ntype OnMessageEventHandlerType = (message: MessageType) => void;\n\nconst version = '1.0.0';\n\nconst createLogger = (onMessage: OnMessageEventHandlerType, parentContext: MessageContextType = {}) => {\n // eslint-disable-next-line id-length\n const log: LoggerType = (a, b, c, d, e, f, g, h, i, k) => {\n const time = Date.now();\n const sequence = global.ROARR.sequence++;\n\n let context;\n let message;\n\n if (typeof a === 'string') {\n context = {\n ...global.ROARR.prepend,\n ...parentContext\n };\n message = sprintf(a, b, c, d, e, f, g, h, i, k);\n } else {\n context = {\n ...global.ROARR.prepend,\n ...parentContext,\n ...a\n };\n\n message = sprintf(b, c, d, e, f, g, h, i, k);\n }\n\n onMessage({\n context,\n message,\n sequence,\n time,\n version\n });\n };\n\n log.child = (context: MessageContextType) => {\n return createLogger(onMessage, {\n ...parentContext,\n ...context\n });\n };\n\n return log;\n};\n\nexport default createLogger;\n"]} | ||
| {"version":3,"sources":["../../src/factories/createLogger.js"],"names":["global","ROARR","version","logLevels","createLogger","onMessage","parentContext","log","a","b","c","d","e","f","g","h","i","k","time","Date","now","sequence","context","message","prepend","child","logLevel"],"mappings":";;;;;;kQAAA;;AAEA;;AAQA;;;;;;;;;;;;;AAEAA,OAAOC,KAAP,GAAeD,OAAOC,KAAP,IAAgB,gDAA/B;;;;;AAIA,IAAMC,UAAU,OAAhB;;AAEA,IAAMC,YAAY,CAChB,OADgB,EAEhB,OAFgB,EAGhB,MAHgB,EAIhB,MAJgB,EAKhB,OALgB,EAMhB,OANgB,CAAlB;;AASA,IAAMC,eAAe,SAAfA,YAAe,CAACC,SAAD,kCAAkF;AAAA,MAA3CC,aAA2C,gGAAP,EAAO;;AACrG;AACA,MAAMC,uBAAkB,SAAlBA,GAAkB,CAACC,CAAD,EAAIC,CAAJ,EAAOC,CAAP,EAAUC,CAAV,EAAaC,CAAb,EAAgBC,CAAhB,EAAmBC,CAAnB,EAAsBC,CAAtB,EAAyBC,CAAzB,EAA4BC,CAA5B,EAAkC;AACxD,QAA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CAFd,EAEiBC,CAFjB,EAEoBC,CAFpB,EAEuBC,CAFvB,CAAP;AAGD,KAJD;AA1CmG;;AAAA;AAAA;AAAA;;AAAA;AAyCrG,yBAAuBd,SAAvB,8HAAkC;AAAA,UAAvBuB,QAAuB;;AAAA,YAAvBA,QAAuB;AAMjC;AA/CoG;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAiDrG,SAAOnB,GAAP;AACD,CAlDD;;kBAoDeH,Y","file":"createLogger.js","sourcesContent":["// @flow\n\nimport {\n sprintf\n} from 'sprintf-js';\nimport type {\n LoggerType,\n MessageContextType,\n MessageType\n} from '../types';\nimport createRoarrInititialGlobalState from './createRoarrInititialGlobalState';\n\nglobal.ROARR = global.ROARR || createRoarrInititialGlobalState();\n\ntype OnMessageEventHandlerType = (message: MessageType) => void;\n\nconst version = '1.0.0';\n\nconst logLevels = [\n 'trace',\n 'debug',\n 'info',\n 'warn',\n 'error',\n 'fatal'\n];\n\nconst createLogger = (onMessage: OnMessageEventHandlerType, parentContext: MessageContextType = {}) => {\n // eslint-disable-next-line id-length\n const log: LoggerType = (a, b, c, d, e, f, g, h, i, k) => {\n const time = Date.now();\n const sequence = global.ROARR.sequence++;\n\n let context;\n let message;\n\n if (typeof a === 'string') {\n context = {\n ...global.ROARR.prepend,\n ...parentContext\n };\n message = sprintf(a, b, c, d, e, f, g, h, i, k);\n } else {\n context = {\n ...global.ROARR.prepend,\n ...parentContext,\n ...a\n };\n\n message = sprintf(b, c, d, e, f, g, h, i, k);\n }\n\n onMessage({\n context,\n message,\n sequence,\n time,\n version\n });\n };\n\n log.child = (context: MessageContextType) => {\n return createLogger(onMessage, {\n ...parentContext,\n ...context\n });\n };\n\n for (const logLevel of logLevels) {\n log[logLevel] = (a, b, c, d, e, f, g, h, i, k) => {\n return log.child({\n logLevel\n })(a, b, c, d, e, f, g, h, i, k);\n };\n }\n\n return log;\n};\n\nexport default createLogger;\n"]} |
+1
-1
@@ -71,3 +71,3 @@ { | ||
| }, | ||
| "version": "1.1.0" | ||
| "version": "1.2.0" | ||
| } |
+121
-43
@@ -15,4 +15,14 @@ # Roarr | ||
| * [jq primer](#jq-primer) | ||
| * [API](#api) | ||
| * [`child`](#child) | ||
| * [`trace`](#trace) | ||
| * [`debug`](#debug) | ||
| * [`info`](#info) | ||
| * [`warn`](#warn) | ||
| * [`error`](#error) | ||
| * [`fatal`](#fatal) | ||
| * [Transports](#transports) | ||
| * [Environment variables](#environment-variables) | ||
| * [Conventions](#conventions) | ||
| * [Using Roarr in an application](#using-roarr-in-an-application) | ||
@@ -28,44 +38,3 @@ ## Usage | ||
| `roarr` package exports a function that accepts the following API: | ||
| ```js | ||
| export type LoggerType = | ||
| ( | ||
| context: MessageContextType, | ||
| message: string, | ||
| c?: SprintfArgumentType, | ||
| d?: SprintfArgumentType, | ||
| e?: SprintfArgumentType, | ||
| f?: SprintfArgumentType, | ||
| g?: SprintfArgumentType, | ||
| h?: SprintfArgumentType, | ||
| i?: SprintfArgumentType, | ||
| k?: SprintfArgumentType | ||
| ) => void | | ||
| ( | ||
| message: string, | ||
| b?: SprintfArgumentType, | ||
| c?: SprintfArgumentType, | ||
| d?: SprintfArgumentType, | ||
| e?: SprintfArgumentType, | ||
| f?: SprintfArgumentType, | ||
| g?: SprintfArgumentType, | ||
| h?: SprintfArgumentType, | ||
| i?: SprintfArgumentType, | ||
| k?: SprintfArgumentType | ||
| ) => void; | ||
| ``` | ||
| Put it into words: | ||
| 1. First parameter can be either a string (message) or an object. | ||
| * If first parameter is an object (context), the second parameter must be a string (message). | ||
| 1. Arguments after the message parameter are used to enable [printf message formatting](https://en.wikipedia.org/wiki/Printf_format_string). | ||
| * Printf arguments must be of a primitive type (`string | number | boolean | null`). | ||
| * There can be up to 9 printf arguments (or 8 if the first parameter is the context object). | ||
| <!-- --> | ||
| ```js | ||
| import log from 'roarr'; | ||
@@ -77,4 +46,2 @@ | ||
| // Creates a child logger appending the provided `context` object | ||
| // to the previous logger context. | ||
| const debug = log.child({ | ||
@@ -188,2 +155,77 @@ level: 'debug' | ||
| ## API | ||
| `roarr` package exports a function that accepts the following API: | ||
| ```js | ||
| export type LoggerType = | ||
| ( | ||
| context: MessageContextType, | ||
| message: string, | ||
| c?: SprintfArgumentType, | ||
| d?: SprintfArgumentType, | ||
| e?: SprintfArgumentType, | ||
| f?: SprintfArgumentType, | ||
| g?: SprintfArgumentType, | ||
| h?: SprintfArgumentType, | ||
| i?: SprintfArgumentType, | ||
| k?: SprintfArgumentType | ||
| ) => void | | ||
| ( | ||
| message: string, | ||
| b?: SprintfArgumentType, | ||
| c?: SprintfArgumentType, | ||
| d?: SprintfArgumentType, | ||
| e?: SprintfArgumentType, | ||
| f?: SprintfArgumentType, | ||
| g?: SprintfArgumentType, | ||
| h?: SprintfArgumentType, | ||
| i?: SprintfArgumentType, | ||
| k?: SprintfArgumentType | ||
| ) => void; | ||
| ``` | ||
| Put it into words: | ||
| 1. First parameter can be either a string (message) or an object. | ||
| * If first parameter is an object (context), the second parameter must be a string (message). | ||
| 1. Arguments after the message parameter are used to enable [printf message formatting](https://en.wikipedia.org/wiki/Printf_format_string). | ||
| * Printf arguments must be of a primitive type (`string | number | boolean | null`). | ||
| * There can be up to 9 printf arguments (or 8 if the first parameter is the context object). | ||
| Refer to the [Usage](#usage) documentation for common usage examples. | ||
| ### `child` | ||
| Creates a child logger appending the provided `context` object to the previous logger context. | ||
| ```js | ||
| type ChildType = (context: MessageContextType) => LoggerType; | ||
| ``` | ||
| ### `trace` | ||
| ### `debug` | ||
| ### `info` | ||
| ### `warn` | ||
| ### `error` | ||
| ### `fatal` | ||
| Convenience methods for logging a message with `logLevel` context property value set to the name of the convenience method, e.g. | ||
| ```js | ||
| import log from 'roarr'; | ||
| log.debug('foo'); | ||
| ``` | ||
| Produces output: | ||
| ``` | ||
| {"context":{"logLevel":"debug"},"message":"foo","sequence":0,"time":1506776210001,"version":"1.0.0"} | ||
| ``` | ||
| ## Transports | ||
@@ -210,1 +252,37 @@ | ||
| |`ROARR_LOG`|Boolean|Enables/ disables logging.|`false`| | ||
| ## Conventions | ||
| ### Using Roarr in an application | ||
| I recommend to create a file `Logger.js` in the project directory. Use this file to create an child instance of Roarr with context parameters describing the project and the initialisation instance, e.g. | ||
| ```js | ||
| /** | ||
| * @file Example contents of a Logger.js file. | ||
| */ | ||
| import log from 'roarr'; | ||
| import ulid from 'ulid'; | ||
| // Instance ID is useful for correlating logs in high concurrency environment. | ||
| const instanceId = ulid(); | ||
| // The reason we are using `global.ROARR.prepend` as opposed to `roarr#child` | ||
| // is because we want this information to be prepended to all logs, including | ||
| // those of the "my-application" dependencies. | ||
| global.ROARR.prepend = { | ||
| ...global.ROARR.prepend, | ||
| application: 'my-application', | ||
| instanceId | ||
| }; | ||
| const Logger = log.child({ | ||
| // .foo property is going to appear only in the logs that are created using | ||
| // the current instance of a Roarr logger. | ||
| foo: 'bar' | ||
| }); | ||
| export default Logger; | ||
| ``` |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
25942
17.71%171
21.28%284
37.86%