applay-utils 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +3 -1
- package/libs-custom/pino-caller.js +51 -0
- package/package.json +2 -1
- package/utils/logger.js +22 -0
package/index.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
var tools = require('./utils/tools.js');
|
|
2
2
|
var querys = require('./utils/querys.js');
|
|
3
|
+
var pino = require('pino');
|
|
3
4
|
|
|
4
5
|
var utils = Object.assign({
|
|
5
6
|
mdb:require('./utils/mdb'),
|
|
6
7
|
crypto:require('./utils/crypto'),
|
|
7
8
|
jwt:require('./utils/jwt'),
|
|
8
|
-
sftp:require('./utils/sftp')
|
|
9
|
+
sftp:require('./utils/sftp'),
|
|
10
|
+
logger:require('./utils/logger')
|
|
9
11
|
},tools,querys);
|
|
10
12
|
|
|
11
13
|
module.exports = utils;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
require('source-map-support/register')
|
|
4
|
+
const NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10)
|
|
5
|
+
const STACKTRACE_OFFSET = NODEJS_VERSION && NODEJS_VERSION > 6 ? 0 : 1
|
|
6
|
+
const LINE_OFFSET = 7
|
|
7
|
+
const { symbols } = require('pino')
|
|
8
|
+
const { asJsonSym } = symbols
|
|
9
|
+
|
|
10
|
+
function traceCaller (pinoInstance, options = {relativeTo: null, stackAdjustment: 0}) {
|
|
11
|
+
const adjustment = options.stackAdjustment || 0
|
|
12
|
+
|
|
13
|
+
function get (target, name) {
|
|
14
|
+
return name === asJsonSym ? asJson : target[name]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function asJson (...args) {
|
|
18
|
+
args[0] = args[0] || Object.create(null)
|
|
19
|
+
args[0].caller = Error().stack.split('\n').slice(2).filter(s => !s.includes('node_modules/pino') && !s.includes('node_modules\\pino'))[STACKTRACE_OFFSET + adjustment].substr(LINE_OFFSET)
|
|
20
|
+
if (options && typeof options.relativeTo === 'string') {
|
|
21
|
+
const lastChar = options.relativeTo[options.relativeTo.length - 1]
|
|
22
|
+
const hasTrailingSlash = lastChar === '/' || lastChar === '\\'
|
|
23
|
+
|
|
24
|
+
args[0].caller = args[0].caller
|
|
25
|
+
.replace(hasTrailingSlash ? options.relativeTo : options.relativeTo + '/', '')
|
|
26
|
+
.replace(hasTrailingSlash ? options.relativeTo : options.relativeTo + '\\', '');
|
|
27
|
+
|
|
28
|
+
const position = args[0].caller.split('<anonymous>:')[1].replace(')','').split(':');
|
|
29
|
+
|
|
30
|
+
args[0].caller = `Linha:${position[0]} - Coluna:${position[1]}`
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
return pinoInstance[asJsonSym].apply(this, args)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return new Proxy(pinoInstance, { get })
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* These export configurations enable JS and TS developers
|
|
41
|
+
* to consumer pino-caller in whatever way best suits their needs.
|
|
42
|
+
* Some examples of supported import syntax includes:
|
|
43
|
+
* - `const pinoCaller = require('pino-caller')`
|
|
44
|
+
* - `const { pinoCaller } = require('pino-caller')`
|
|
45
|
+
* - `import * as pinoCaller from 'pino-caller'`
|
|
46
|
+
* - `import { pinoCaller } from 'pino-caller'`
|
|
47
|
+
* - `import pinoCaller from 'pino-caller'`
|
|
48
|
+
*/
|
|
49
|
+
traceCaller.pinoCaller = traceCaller
|
|
50
|
+
traceCaller.default = traceCaller
|
|
51
|
+
module.exports = traceCaller
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applay-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Utilitary tools for Applay applications",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "npm version patch",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"jsonwebtoken": "^8.5.1",
|
|
20
20
|
"mongodb": "^4.12.1",
|
|
21
21
|
"pg": "^8.8.0",
|
|
22
|
+
"pino": "^8.15.0",
|
|
22
23
|
"ssh2-sftp-client": "^9.0.4"
|
|
23
24
|
}
|
|
24
25
|
}
|
package/utils/logger.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var pino = require('pino');
|
|
2
|
+
|
|
3
|
+
var logger = {
|
|
4
|
+
create: (filelog,nivel,prefix)=>{
|
|
5
|
+
var modLogger = !filelog? pino():pino(pino.destination(`./logs/${filelog}.log`));
|
|
6
|
+
var pinoCaller = require('../libs-custom/pino-caller')(modLogger, { stackAdjustment: nivel||1 })
|
|
7
|
+
var consoleFunc = {
|
|
8
|
+
log: (...args) => {
|
|
9
|
+
if (typeof args[0] != 'string') { args.unshift(''); }
|
|
10
|
+
var msg = `[ ${prefix} ] - ${args[0]}`;
|
|
11
|
+
if (args.length > 1) {
|
|
12
|
+
return pinoCaller.info(args.slice(1), msg)
|
|
13
|
+
} else {
|
|
14
|
+
return pinoCaller.info(msg)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return consoleFunc;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports = logger;
|