//////////////////////////////////////////////////////// // Adds prototype inspection function addInspection(type, cb) { type.prototype[npm.util.inspect.custom] = cb; }
function inspect(obj) { return obj[util.inspect.custom](); }
[util.inspect.custom]() { // http://stackoverflow.com/questions/5905492/dynamic-function-name-in-javascript const copy = new {[this.constructor.name]: class {}}[this.constructor.name](); for(const key in this) { if(this.hasOwnProperty(key) && !key.startsWith("_") && this[key] !== undefined) { copy[key] = this[key]; } } return copy; }
function handler(msg, reply, next) { var type = msg.type ? msg.type : "unknown message"; type = msg.edited ? ("Edited " + type) : capitalize(type); console.log("\n%s at %s %s (%s):", type, msg.chat.type, msg.chat.id, msg.chat.name); console.log(util.inspect(msg, {colors: true, depth: null})); }
prettifyObject(obj) { return util.inspect(obj, { depth: null, colors: true, breakLength: 90 }); }
it('普通日志记录', function() { const log = createLogger('Test'); log.info('init service: %s', 'test'); Logger.handler.calledOnce.should.be.true(); Logger.handler.getCall(0).args.should.eql(['Test', 'info', 'init service: test']); const o = { name: 'test' }; log.info('data: %o', o); Logger.handler.callCount.should.equal(2); getMessage(1).should.equal('data: ' + util.inspect(o)); log.debug('some debug message'); Logger.handler.callCount.should.equal(2); });
function resolve(data) { debug('in resolve'); debug('data %s', data); debug('resolve this: %s', util.inspect(this, false, 20, true)); return (typeof data === 'string'); }
const getResData = async (origin, path) => { const href = `${origin}${path}`; log.debug(`[getResData] GET ${href}`); try { const res = await axios.get(href); log.trace(`[getResData] GET ${href} Received res.data: ${inspect(res.data)}`); return res.data; } catch (error) { throw new Error(`GET ${href} failed: ${inspect(error.response.data)}`); } }
// Function which logs any value function log(value) { console.log(util.inspect(value, false, null)); }
[util.inspect.custom] (depth, options) { if (this.id) return `Peer {id=${this.id}}` return `Peer {addr=${this.remoteAddress}:${this.remotePort}}` }
const makePostRequest = async (origin, path) => { const href = `${origin}${path}`; log.debug(`[makePostRequest] POST ${href}`); try { const res = await axios.post(href); log.trace(`[makePostRequest] POST ${href} Received res.data: ${inspect(res.data)}`); return res.data; } catch (error) { throw new Error(`POST ${href} failed: ${inspect(error.response.data)}`); } }