utils: refine logger config/import

This commit is contained in:
Micooz 2017-08-20 22:01:30 +08:00
parent 454cc88cd1
commit 1ffb7800de
10 changed files with 34 additions and 69 deletions

8
.gitignore vendored

@ -10,6 +10,7 @@ logs/
debug/
npm-debug.log*
yarn-error.log
*.log.*
# test
coverage/
@ -18,9 +19,6 @@ coverage/
*.tgz
# others
blinksocks*.js
blinksocks*.json
blinksocks.client.js
blinksocks.server.js
blinksocks*.log
/*.log
/build/blinksocks.js.map
*.map

@ -1,13 +1,7 @@
import {Logger} from '../utils';
let logger = null;
import {logger} from '../utils';
export default class DirectCloseBehaviour {
constructor() {
logger = Logger.getInstance();
}
async run({remoteHost, remotePort, onClose}) {
logger.warn(`[behaviour] [${remoteHost}:${remotePort}] connection closed`);
onClose();

@ -1,10 +1,8 @@
import {getRandomInt, Logger} from '../utils';
import {getRandomInt, logger} from '../utils';
const DEFAULT_TIMEOUT_MIN = 10;
const DEFAULT_TIMEOUT_MAX = 40;
let logger = null;
export default class RandomTimeoutBehaviour {
min = DEFAULT_TIMEOUT_MIN;
@ -12,7 +10,6 @@ export default class RandomTimeoutBehaviour {
max = DEFAULT_TIMEOUT_MAX;
constructor({min, max}) {
logger = Logger.getInstance();
if (min !== undefined) {
if (!Number.isInteger(min)) {
throw Error('\'min\' must be an integer');

@ -1,6 +1,4 @@
import {Logger, isValidHostname, isValidPort} from '../utils';
let logger = null;
import {logger, isValidHostname, isValidPort} from '../utils';
export default class RedirectBehaviour {
@ -9,7 +7,6 @@ export default class RedirectBehaviour {
_port = '';
constructor({host, port}) {
logger = Logger.getInstance();
if (!isValidHostname(host)) {
throw Error('\'host\' is invalid');
}

@ -1,5 +1,5 @@
import net from 'net';
import {Logger} from '../utils';
import {logger} from '../utils';
const QUERY_INTERVAL = 12e4; // 2min
@ -70,7 +70,6 @@ export class Balancer {
}
static _query() {
const logger = Logger.getInstance();
this._servers.map((server, i) => {
const sstr = `${server.host}:${server.port}`;
logger.verbose(`[balancer] querying ${sstr}`);

@ -3,10 +3,11 @@ import fs from 'fs';
import path from 'path';
import os from 'os';
import net from 'net';
import winston from 'winston';
import isPlainObject from 'lodash.isplainobject';
import {getBehaviourClassByName, BEHAVIOUR_EVENT_ON_PRESET_FAILED, behaviourEvents} from '../behaviours';
import {getPresetClassByName} from '../presets';
import {isValidHostname, isValidPort, Logger} from '../utils';
import {isValidHostname, isValidPort, logger} from '../utils';
import {DNS_DEFAULT_EXPIRE} from './dns-cache';
export const DEFAULT_LOG_LEVEL = 'info';
@ -237,10 +238,29 @@ export class Config {
// log_path & log_level
const absolutePath = path.resolve(process.cwd(), json.log_path || '.');
const isFile = fs.statSync(absolutePath).isFile();
let isFile = false;
if (fs.existsSync(absolutePath)) {
isFile = fs.statSync(absolutePath).isFile();
} else if (path.extname(absolutePath) !== '') {
isFile = true;
}
global.__LOG_PATH__ = isFile ? absolutePath : path.join(absolutePath, `bs-${__IS_CLIENT__ ? 'client' : 'server'}.log`);
global.__LOG_LEVEL__ = (json.log_level !== undefined) ? json.log_level : DEFAULT_LOG_LEVEL;
Logger.init({file: __LOG_PATH__, level: __LOG_LEVEL__});
logger.configure({
level: __LOG_LEVEL__,
transports: [
new (winston.transports.Console)({
colorize: true,
prettyPrint: true
}),
new (require('winston-daily-rotate-file'))({
filename: __LOG_PATH__,
level: __LOG_LEVEL__
})
]
});
// behaviours
const behaviours = {

@ -4,9 +4,7 @@ import net from 'net';
import {Config} from './config';
import {Socket} from './socket';
import {Balancer} from './balancer';
import {Logger} from '../utils';
let logger = null;
import {logger} from '../utils';
const nextId = (function () {
let i = 0;
@ -41,7 +39,6 @@ export class Hub extends EventEmitter {
if (typeof config !== 'undefined') {
Config.init(config);
}
logger = Logger.getInstance();
this._hub = net.createServer();
this._hub.on('close', this.onClose.bind(this));
this._hub.on('connection', this.onConnect.bind(this));

@ -1,6 +1,6 @@
import EventEmitter from 'events';
import net from 'net';
import {Logger, isValidHostname, isValidPort} from '../utils';
import {logger, isValidHostname, isValidPort} from '../utils';
import {Config} from './config';
import {DNSCache} from './dns-cache';
import {Balancer} from './balancer';
@ -22,7 +22,6 @@ import {BEHAVIOUR_EVENT_ON_PRESET_FAILED} from '../behaviours';
const MAX_BUFFERED_SIZE = 1024 * 1024; // 1MB
let logger = null;
let lastServer = null;
function selectServer() {
@ -61,7 +60,6 @@ export class Socket extends EventEmitter {
constructor({socket}) {
super();
logger = Logger.getInstance();
this.onForward = this.onForward.bind(this);
this.onBackward = this.onBackward.bind(this);
this.onError = this.onError.bind(this);

@ -1,7 +1,5 @@
import {IPreset, CONNECTION_CREATED, SOCKET_CONNECT_TO_REMOTE, CONNECTION_CLOSED} from './defs';
import {Logger} from '../utils';
let logger = null;
import {logger} from '../utils';
const TRACK_CHAR_UPLOAD = 'u';
const TRACK_CHAR_DOWNLOAD = 'd';
@ -15,11 +13,6 @@ export default class TrackerPreset extends IPreset {
// ['source', 'target', 'u', '20', 'u', '20', 'd', '10', ...]
_tracks = [];
constructor() {
super();
logger = Logger.getInstance();
}
onNotified({type, payload}) {
switch (type) {
case CONNECTION_CREATED:

@ -1,30 +1,2 @@
import winston from 'winston';
const LOG_FILE_MAX_SIZE = 2 * 1024 * 1024; // 2MB
let instance = null;
export const Logger = {
init({file, level}) {
instance = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
colorize: true,
prettyPrint: true
}),
new (winston.transports.File)({
filename: file,
maxsize: LOG_FILE_MAX_SIZE,
silent: ['test', 'debug'].includes(process.env.NODE_ENV)
})
]
});
instance.level = level;
},
getInstance() {
return instance || winston;
}
};
export const logger = new (winston.Logger)();