Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function register (server, options) {
// clone all user options to account for internal mutations, except for existing stream and pino instances
options = Object.assign(Hoek.clone(options), {
stream: options.stream,
instance: options.instance
})
options.serializers = options.serializers || {}
options.serializers.req = stdSerializers.wrapRequestSerializer(options.serializers.req || stdSerializers.req)
options.serializers.res = stdSerializers.wrapResponseSerializer(options.serializers.res || stdSerializers.res)
options.serializers.err = options.serializers.err || pino.stdSerializers.err
if (options.logEvents === undefined) {
options.logEvents = ['onPostStart', 'onPostStop', 'response', 'request-error']
}
var logger
if (options.instance) {
logger = options.instance
const overrideDefaultErrorSerializer =
typeof options.serializers.err === 'function' && logger[serializersSym].err === stdSerializers.err
logger[serializersSym] = Object.assign({}, options.serializers, logger[serializersSym])
if (overrideDefaultErrorSerializer) {
logger[serializersSym].err = options.serializers.err
}
} else {
options.stream = options.stream || process.stdout
exports.log = function esLogger (config = {}) {
// config is the object passed to the client constructor.
const logger = pino({
name: 'Leaistic ↔ ElasticSearch',
serializers: {
err: pino.stdSerializers.err
},
level: ES_LOG_THRESHOLD,
...config
})
this.error = (message, object, ...rest) => logger.error(object, message, ...rest)
this.warning = (message, object, ...rest) => logger.warn(object, message, ...rest)
this.info = (message, object, ...rest) => logger.info(object, message, ...rest)
this.debug = (message, object, ...rest) => logger.debug(object, message, ...rest)
// ES trace mode is used to track HTTP requests, which tends to be actually more important than `debug` level content
// pino has some standard format ( from default serializers) for `req` and `res` that we can leverage to have nice looking logs
this.trace = (method, req, body, responseBody, statusCode) => {
const level = statusCode < 500 ? ES_LOG_LEVEL_OK : ES_LOG_LEVEL_ERROR
const {protocol, hostname, port, path, headers} = req
const message = 'request completed'
const SecretsManagerBackend = require('../lib/backends/secrets-manager-backend')
const SystemManagerBackend = require('../lib/backends/system-manager-backend')
const VaultBackend = require('../lib/backends/vault-backend')
// Get document, or throw exception on error
// eslint-disable-next-line security/detect-non-literal-fs-filename
const customResourceManifest = yaml.safeLoad(fs.readFileSync(path.resolve(__dirname, '../crd.yaml'), 'utf8'))
const kubeconfig = new kube.KubeConfig()
kubeconfig.loadFromDefault()
const kubeBackend = new KubeRequest({ kubeconfig })
const kubeClient = new kube.Client({ backend: kubeBackend })
const logger = pino({
serializers: {
err: pino.stdSerializers.err
},
level: envConfig.logLevel
})
const customResourceManager = new CustomResourceManager({
kubeClient,
logger
})
const secretsManagerBackend = new SecretsManagerBackend({
clientFactory: awsConfig.secretsManagerFactory,
assumeRole: awsConfig.assumeRole,
logger
})
const systemManagerBackend = new SystemManagerBackend({
clientFactory: awsConfig.systemManagerFactory,
component,
mkconfig,
optrequire,
} from './private'
/**
* This class represents your application and aggregates all components together
*
* You should generally create only one instance of this class in a program.
*/
class Application {
static defaults = {
log: {
name: path.basename(process.cwd()),
level: 'info',
serializers: pino.stdSerializers,
},
}
/**
* Initialise a brand-new app from the given module locations
*
* @param {Object} options Configuration options
* @param {String} options.env The environment under which to operate
* @param {String} options.root The root directory to which all other
* directories mentioned here are relative
* @param {String} options.config='config' Module location for the configuration
* @param {String} options.hooks='hooks' Module location for the hooks
* @param {String} options.services='services' Module location for the services
* @param {String} options.actions='actions' Module location for the actions
* @param {String} options.aliases='aliases' Module location for the aliases
* @return {Application}
function definitionSerializer(definition) {
return {
resourceName: definition.name
};
}
function reqSerializer(req) {
return Object.assign(pino.stdSerializers.req(req), {
reqId: req.id
});
}
const log = pino({
name: 'api-pls-server',
serializers: Object.assign({}, pino.stdSerializers, {
req: reqSerializer,
definition: definitionSerializer
}),
src: isDevEnv,
prettyPrint: isDevEnv
});
module.exports = log;
const pino = require('pino')
const {req, res, err} = pino.stdSerializers
const serializers = {req, res, err}
exports._log = undefined
exports.log = (logger) => {
if (logger) {
exports._log = logger
}
if (!exports._log) {
exports._log = pino({ name: 'Leaistic', safe: true, level: 'debug', serializers })
}
return exports._log
}
createRequestsLogger(options, destination) {
return koaPinoLogger(
Object.assign(
{
logger: this.createLogger(
options,
destination || pino.destination(path.join(logPath, 'requests.log'))
),
serializers: {
req: pino.stdSerializers.req,
res: pino.stdSerializers.res,
},
},
options
)
);
}
exports.createPinoLogger = function ( name, options = {} ) {
var logger = pino({
name: name,
prettyPrint: !!options.prettyPrint,
level: options.level || 'info',
serializers: {
req: pino.stdSerializers.req,
res: pino.stdSerializers.res
}
})
return logger
}
return logger
}
const serializers = {
req: function asReqValue (req) {
return {
method: req.method,
url: req.url,
version: req.headers['accept-version'],
hostname: req.hostname,
remoteAddress: req.ip,
remotePort: req.connection.remotePort
}
},
err: pino.stdSerializers.err,
res: function asResValue (res) {
return {
statusCode: res.statusCode
}
}
}
function now () {
var ts = process.hrtime()
return (ts[0] * 1e3) + (ts[1] / 1e6)
}
function createLogger (options) {
if (isValidLogger(options.logger)) {
const logger = createPinoLogger({
logger: options.logger,
function reqSerializer(req) {
return Object.assign(pino.stdSerializers.req(req), {
reqId: req.id
});
}