0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

fix: using correctly logging levels (#2153)

* fix: using correctly logging levels

* chore: update deps
This commit is contained in:
Juan Picado 2021-03-31 21:21:40 +02:00 committed by GitHub
parent 99e10f57cf
commit 5b6be2aa09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 48 additions and 46 deletions

View file

@ -20,10 +20,8 @@ jobs:
with:
node-version: ${{ matrix.node_version }}
- name: Install
run: yarn install --immutable
run: yarn install
- name: Build
run: yarn code:build
- name: Test CLI
run: yarn test:e2e:cli
env:
NODE_ENV: production

View file

@ -41,8 +41,10 @@
"jsonwebtoken": "8.5.1",
"kleur": "4.1.4",
"lodash": "4.17.21",
"lru-cache": "6.0.0",
"lunr-mutable-indexes": "2.3.2",
"marked": "2.0.1",
"memoizee": "0.4.15",
"mime": "2.5.2",
"minimatch": "3.0.4",
"mkdirp": "1.0.4",
@ -126,7 +128,6 @@
"jest-junit": "9.0.0",
"lint-staged": "8.2.1",
"lockfile-lint": "4.3.7",
"lru-cache": "6.0.0",
"nock": "12.0.3",
"node-mocks-http": "^1.10.1",
"prettier": "2.2.1",
@ -171,6 +172,7 @@
"lint:ts": "eslint \"**/*.{js,jsx,ts,tsx}\"",
"lint:lockfile": "lockfile-lint --path yarn.lock --type yarn --validate-https --allowed-hosts verdaccio npm yarn",
"start": "yarn babel-node --extensions \".ts,.tsx\" src/lib/cli",
"start:debug": "yarn node debug/bootstrap.js",
"code:build": "yarn babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps inline",
"code:docker-build": "yarn babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\"",
"docker": "docker build -t verdaccio/verdaccio:pr-2122 . --no-cache",

View file

@ -227,9 +227,6 @@ export const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{
export function log(config: Config) {
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
// logger
req.log = logger.child({ sub: 'in' });
const _auth = req.headers.authorization;
if (_.isNil(_auth) === false) {
req.headers.authorization = '<Classified>';
@ -243,7 +240,7 @@ export function log(config: Config) {
req.url = req.originalUrl;
// avoid log noise data from static content
if (req.originalUrl.match(/static/) === null) {
req.log.info({ req: req, ip: req.ip }, "@{ip} requested '@{req.method} @{req.url}'");
logger.http({ req: req, ip: req.ip }, "@{ip} requested '@{req.method} @{req.url}'");
}
req.originalUrl = req.url;
@ -293,13 +290,12 @@ export function log(config: Config) {
req.url = req.originalUrl;
// avoid log noise data from static content
if (req.url.match(/static/) === null) {
req.log.warn(
logger.http(
{
request: {
method: req.method,
url: req.url,
},
level: 35, // http
user: (req.remote_user && req.remote_user.name) || null,
remoteIP,
status: res.statusCode,

View file

@ -18,6 +18,7 @@ import loadPlugin from '../lib/plugin-loader';
import { $RequestExtend, $ResponseExtend, IAuth, AESPayload } from '../../types';
import { API_ERROR, SUPPORT_ERRORS, TOKEN_BASIC, TOKEN_BEARER } from './constants';
import { aesEncrypt, signPayload } from './crypto-utils';
import { logger } from './logger';
import {
getDefaultPlugins,
getMiddlewareCredentials,
@ -33,9 +34,6 @@ import {
import { convertPayloadToBase64, ErrorCode } from './utils';
import { getMatchedPackagesSpec } from './config-utils';
/* eslint-disable @typescript-eslint/no-var-requires */
const LoggerApi = require('./logger');
class Auth implements IAuth {
public config: Config;
public logger: Logger;
@ -44,7 +42,7 @@ class Auth implements IAuth {
public constructor(config: Config) {
this.config = config;
this.logger = LoggerApi.logger.child({ sub: 'auth' });
this.logger = logger;
this.secret = config.secret;
this.plugins = this._loadPlugin(config);
this._applyDefaultPlugins();

View file

@ -48,7 +48,7 @@ class LocalStorage implements IStorage {
public logger: Logger;
public constructor(config: Config, logger: Logger) {
this.logger = logger.child({ sub: 'fs' });
this.logger = logger;
this.config = config;
this.storagePlugin = this._loadStorage(config, logger);
}

View file

@ -6,18 +6,20 @@ export type LevelCode = number;
export function calculateLevel(levelCode: LevelCode): LogLevel {
switch (true) {
case levelCode < 15:
case levelCode === 10:
return 'trace';
case levelCode < 25:
case levelCode === 20:
return 'debug';
case levelCode < 35:
return 'info';
case levelCode == 35:
case levelCode === 25:
return 'http';
case levelCode < 45:
case levelCode === 30:
return 'info';
case levelCode === 40:
return 'warn';
case levelCode < 55:
case levelCode === 50:
return 'error';
case levelCode === 60:
return 'fatal';
default:
return 'fatal';
}

View file

@ -21,7 +21,7 @@ export type LogType = 'file' | 'stdout';
export type LogFormat = 'json' | 'pretty-timestamped' | 'pretty';
export function createLogger(
options = {},
options = {level: 'http'},
destination = pino.destination(1),
format: LogFormat = DEFAULT_LOG_FORMAT,
prettyPrintOptions = {
@ -35,10 +35,11 @@ export function createLogger(
}
let pinoConfig = {
...options,
customLevels: {
http: 35,
http: 25,
},
...options,
level: options.level,
serializers: {
err: pino.stdSerializers.err,
req: pino.stdSerializers.req,
@ -60,13 +61,20 @@ export function createLogger(
prettifier: require('./formatter'),
});
}
const logger = pino(pinoConfig, destination);
return pino(pinoConfig, destination);
if(process.env.DEBUG) {
logger.on('level-change', (lvl, val, prevLvl, prevVal) => {
debug('%s (%d) was changed to %s (%d)', lvl, val, prevLvl, prevVal);
})
}
return logger;
}
export function getLogger() {
if (_.isNil(logger)) {
console.warn('logger is not defined');
process.emitWarning('logger is not defined');
return;
}
@ -94,8 +102,7 @@ export function setup(options: LoggerConfig | LoggerConfigItem = [DEFAULT_LOGGER
const isLegacyConf = Array.isArray(options);
if (isLegacyConf) {
const deprecateMessage = 'deprecate: multiple logger configuration is deprecated, please check the migration guide.';
// eslint-disable-next-line no-console
console.log(yellow(padLeft(deprecateMessage)));
process.emitWarning(deprecateMessage);
}
// verdaccio 5 does not allow multiple logger configuration
@ -103,20 +110,16 @@ export function setup(options: LoggerConfig | LoggerConfigItem = [DEFAULT_LOGGER
// next major will thrown an error
let loggerConfig = isLegacyConf ? options[0] : options;
if (!loggerConfig?.level) {
loggerConfig = Object.assign({}, loggerConfig, {
loggerConfig = Object.assign({}, {
level: 'http',
});
}, loggerConfig);
}
const pinoConfig = { level: loggerConfig.level };
if (loggerConfig.type === 'file') {
debug('logging file enabled');
logger = createLogger(pinoConfig, pino.destination(loggerConfig.path), loggerConfig.format);
} else if (loggerConfig.type === 'rotating-file') {
// eslint-disable-next-line no-console
console.log(yellow(padLeft('rotating-file type is not longer supported, consider use [logrotate] instead')));
// eslint-disable-next-line no-console
console.log(yellow(padLeft('fallback to stdout configuration triggered')));
process.emitWarning('rotating-file type is not longer supported, consider use [logrotate] instead');
debug('logging stdout enabled');
logger = createLogger(pinoConfig, pino.destination(1), loggerConfig.format);
} else {

View file

@ -55,7 +55,7 @@ class Storage implements IStorageHandler {
public constructor(config: Config) {
this.config = config;
this.uplinks = setupUpLinks(config);
this.logger = logger.child({module: 'storage'});
this.logger = logger;
this.filters = [];
// @ts-ignore
this.localStorage = null;

View file

@ -8,6 +8,7 @@ import { ReadTarball } from '@verdaccio/streams';
import { Config, Callback, Headers, Logger, Package } from '@verdaccio/types';
import { IProxy, UpLinkConfLocal } from '../../types';
import { parseInterval, isObject, ErrorCode, buildToken } from './utils';
import { logger} from './logger';
import {
ERROR_CODE,
TOKEN_BASIC,
@ -18,7 +19,6 @@ import {
HEADER_TYPE,
CHARACTER_ENCODING
} from './constants';
const LoggerApi = require('./logger');
const encode = function (thing): string {
return encodeURIComponent(thing).replace(/^%40/, '@');
@ -71,7 +71,7 @@ class ProxyStorage implements IProxy {
this.failed_requests = 0;
this.userAgent = mainConfig.user_agent;
this.ca = config.ca;
this.logger = LoggerApi.logger.child({ sub: 'out' });
this.logger = logger;
this.server_id = mainConfig.server_id;
this.url = URL.parse(this.config.url);
@ -194,11 +194,10 @@ class ProxyStorage implements IProxy {
function logActivity(): void {
let message = "@{!status}, req: '@{request.method} @{request.url}'";
message += error ? ', error: @{!error}' : ', bytes: @{bytes.in}/@{bytes.out}';
self.logger.warn(
self.logger.http(
{
err: err || undefined, // if error is null/false change this to undefined so it wont log
request: { method: method, url: uri },
level: 35, // http
status: res != null ? res.statusCode : 'ERR',
error: error,
bytes: {
@ -245,13 +244,12 @@ class ProxyStorage implements IProxy {
if (_.isNil(requestCallback) === false) {
(function do_log(): void {
const message = "@{!status}, req: '@{request.method} @{request.url}' (streaming)";
self.logger.warn(
self.logger.http(
{
request: {
method: method,
url: uri
},
level: 35, // http
status: _.isNull(res) === false ? res.statusCode : 'ERR'
},
message

View file

@ -6,6 +6,7 @@ import buildDebug from 'debug';
import semver from 'semver';
import YAML from 'js-yaml';
import validator from 'validator';
import memoizee from 'memoizee';
import sanitizyReadme from '@verdaccio/readme';
import { Package, Version, Author } from '@verdaccio/types';
@ -143,6 +144,8 @@ export function convertDistRemoteToLocalTarballUrls(pkg: Package, req: Request,
return pkg;
}
const memoizedgetPublicUrl = memoizee(getPublicUrl);
/**
* Filter a tarball url.
* @param {*} uri
@ -155,7 +158,7 @@ export function getLocalRegistryTarballUri(uri: string, pkgName: string, req: Re
return uri;
}
const tarballName = extractTarballFromUrl(uri);
const domainRegistry = getPublicUrl(urlPrefix || '', req);
const domainRegistry = memoizedgetPublicUrl(urlPrefix || '', req);
return `${domainRegistry}${encodeScopedUri(pkgName)}/-/${tarballName}`;
}
@ -368,8 +371,10 @@ export function parseConfigFile(configPath: string): any {
if (/\.ya?ml$/i.test(configPath)) {
return YAML.load(fs.readFileSync(configPath, CHARACTER_ENCODING.UTF8));
}
debug('yaml parsed');
return require(configPath);
} catch (e) {
debug('yaml parse failed');
if (e.code !== 'MODULE_NOT_FOUND') {
e.message = APP_ERROR.CONFIG_NOT_VALID;
}

View file

@ -13,7 +13,7 @@ uplinks:
npmjs:
url: https://registry.verdaccio.org/
logs: { type: stdout, format: json, level: trace }
logs: { type: stdout, format: json, level: http }
packages:
'@*/*':

View file

@ -17,7 +17,7 @@ uplinks:
local:
url: http://localhost:4873
logs: { type: stdout, format: json, level: warn }
logs: { type: stdout, format: json, level: http }
packages:
'@*/*':

BIN
yarn.lock

Binary file not shown.