qcloud-iotexplorer-logger
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createInsightLogger=exports.InsightLogger=void 0;var tslib_1=require("tslib"),error_1=tslib_1.__importDefault(require("./lib/error")),debugLogger_1=tslib_1.__importDefault(require("./lib/debugLogger")),utils_1=require("./utils"),logger_1=require("./logger"),internal_transport_1=require("./internal-transport"),debugLogger=debugLogger_1.default("iot-logger-insight"),ERR_INVALID_ARG=error_1.default.ERR_INVALID_ARG,EVENT_PH="%s",EVENT_PH_REG=new RegExp(EVENT_PH+"$"),InsightLogger=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return tslib_1.__extends(t,e),t}(logger_1.Logger);exports.InsightLogger=InsightLogger;var MAX_DERIVED_LEVEL=5,DEEP_COPY_PROPS={metric:!0,context:!0},copyProps=function(e,t,r){return void 0===r&&(r=0),utils_1.isPlainObject(e)?utils_1.isPlainObject(t)?(Object.keys(t).forEach((function(o){var i=e[o],n=t[o];DEEP_COPY_PROPS[o]&&0===r?e[o]=copyProps(i,n,1):0===r&&"event"===o&&"string"==typeof i&&"string"==typeof n&&i.match(EVENT_PH_REG)?e[o]=i.replace(EVENT_PH_REG,n):e[o]=n})),e):e:utils_1.isPlainObject(t)?t:{}};exports.createInsightLogger=function(e){void 0===e&&(e={});var t=e.level,r=tslib_1.__rest(e,["level"]),o=logger_1.createLogger({level:t});o.add(new internal_transport_1.ConsoleTransport({level:"debug",format:logger_1.format((function(e){return e[internal_transport_1.SPEC_CONSOLE_ARGS]=["["+e.message.event+"]",e.message.message,e.message],e}))()}));var i=function(){};i.prototype=o;var n=function(e){function t(t){var r=t.derivedLevel,o=t.getReportData,i=e.call(this)||this;return i.initFormat(),i.derivedLevel="number"==typeof r&&r>=0?r:1/0,i.updateGetReportData(o),i}return tslib_1.__extends(t,e),t.formatLog=function(e){var t,r=tslib_1.__read(e.splat,1)[0];return utils_1.isPlainObject(e.message)?t=e.message:((t={}).event=String(e.message),"string"==typeof r?t.message=r:utils_1.isPlainObject(r)&&Object.assign(t,r)),t.level=logger_1.LogLevel[e.level],t},t.prototype.updateGetReportData=function(e){"function"==typeof e&&(this.getReportData=e)},t.prototype.derive=function(e){if("function"!=typeof e)throw new ERR_INVALID_ARG("getReportData","function");if(this.derivedLevel+1>=MAX_DERIVED_LEVEL)throw new Error("derived level exceed, current: "+(this.derivedLevel+1)+", max: "+MAX_DERIVED_LEVEL);var r=new t({derivedLevel:this.derivedLevel+1,getReportData:e});return r.parent=this,r},t.prototype.destroy=function(){this.parent=null},t.prototype.initFormat=function(){var e=this,r=logger_1.format((function(r){var o=t.formatLog(r);return(o=e.mergeLogInfo(o))?(r.message=o,r):(debugLogger.warn("initFormat() reportData is null"),!1)}));this.updateFormat(r())},t.prototype.mergeLogInfo=function(e){var t=[];t.push(e);for(var r=this;r;){if(r.getReportData){var o=r.getReportData();utils_1.isPlainObject(o)&&t.push(o)}r=r.parent}for(var i=null,n=t.length-1;n>=0;n--)i=copyProps(i,t[n],0);return i},t}(i);return new n(tslib_1.__assign(tslib_1.__assign({},r),{derivedLevel:0}))}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createInsightLogger=exports.InsightLogger=void 0;var tslib_1=require("tslib"),error_1=tslib_1.__importDefault(require("./lib/error")),debugLogger_1=tslib_1.__importDefault(require("./lib/debugLogger")),utils_1=require("./utils"),logger_1=require("./logger"),internal_transport_1=require("./internal-transport"),debugLogger=debugLogger_1.default("iot-logger-insight"),ERR_INVALID_ARG=error_1.default.ERR_INVALID_ARG,EVENT_PH="%s",EVENT_PH_REG=new RegExp(EVENT_PH+"$"),InsightLogger=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return tslib_1.__extends(t,e),t}(logger_1.Logger);exports.InsightLogger=InsightLogger;var MAX_DERIVED_LEVEL=5,DEEP_COPY_PROPS={metric:!0,context:!0},copyProps=function(e,t,r){return void 0===r&&(r=0),utils_1.isPlainObject(e)?utils_1.isPlainObject(t)?(Object.keys(t).forEach((function(o){var i=e[o],n=t[o];DEEP_COPY_PROPS[o]&&0===r?e[o]=copyProps(i,n,1):0===r&&"event"===o&&"string"==typeof i&&"string"==typeof n&&i.match(EVENT_PH_REG)?e[o]=i.replace(EVENT_PH_REG,n):e[o]=n})),e):e:utils_1.isPlainObject(t)?t:{}},formatLog=function(e){var t,r=tslib_1.__read(e.splat,1)[0];return utils_1.isPlainObject(e.message)?t=e.message:(t={},"string"==typeof r?t.message=r:utils_1.isPlainObject(r)&&Object.assign(t,r),t.event=String(e.message)),t.level=logger_1.LogLevel[e.level],t.at=+new Date,t.level===logger_1.LogLevel.error&&t.error instanceof Error&&(t.error={message:t.error.message,stack:t.error.stack}),t};exports.createInsightLogger=function(e){void 0===e&&(e={});var t=e.level,r=tslib_1.__rest(e,["level"]),o=logger_1.createLogger({level:t});o.add(new internal_transport_1.ConsoleTransport({level:"debug",format:logger_1.format((function(e){return e[internal_transport_1.SPEC_CONSOLE_ARGS]=["["+e.message.event+"]",e.message.message,e.message],e}))()}));var i=function(){};i.prototype=o;var n=function(e){function t(t){var r=t.derivedLevel,o=t.getReportData,i=e.call(this)||this;return i.initFormat(),i.derivedLevel="number"==typeof r&&r>=0?r:1/0,i.updateGetReportData(o),i}return tslib_1.__extends(t,e),t.prototype.updateGetReportData=function(e){"function"==typeof e&&(this.getReportData=e)},t.prototype.derive=function(e){if("function"!=typeof e)throw new ERR_INVALID_ARG("getReportData","function");if(this.derivedLevel+1>=MAX_DERIVED_LEVEL)throw new Error("derived level exceed, current: "+(this.derivedLevel+1)+", max: "+MAX_DERIVED_LEVEL);var r=new t({derivedLevel:this.derivedLevel+1,getReportData:e});return r.parent=this,r},t.prototype.destroy=function(){this.parent=null},t.prototype.initFormat=function(){var e=this,t=logger_1.format((function(t){var r=formatLog(t);return(r=e.mergeLogInfo(r))?(t.message=r,t):(debugLogger.warn("initFormat() reportData is null"),!1)}));this.updateFormat(t())},t.prototype.mergeLogInfo=function(e){var t=[];t.push(e);for(var r=this;r;){if(r.getReportData){var o=r.getReportData();utils_1.isPlainObject(o)&&t.push(o)}r=r.parent}for(var i=null,n=t.length-1;n>=0;n--)i=copyProps(i,t[n],0);return i},t}(i);return new n(tslib_1.__assign(tslib_1.__assign({},r),{derivedLevel:0}))}; | ||
//# sourceMappingURL=InsightLogger.js.map |
@@ -5,6 +5,8 @@ /// <reference types="miniprogram-api-typings" /> | ||
url: string; | ||
skipUpload?: boolean; | ||
} | ||
export declare class ElasticsearchTransport extends LoggerTransport { | ||
skipUpload: boolean; | ||
private readonly url; | ||
constructor({ url, highWaterMark, consumeNum, ...opts }: ElasticsearchTransportOptions); | ||
constructor({ url, skipUpload, highWaterMark, consumeNum, level, ...opts }: ElasticsearchTransportOptions); | ||
protected logBatch(logs: any, cb: any): Promise<void>; | ||
@@ -11,0 +13,0 @@ protected request(uploadData: Record<string, unknown>[]): Promise<WechatMiniprogram.RequestSuccessCallbackResult | { |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ElasticsearchTransport=void 0;var tslib_1=require("tslib"),logger_1=require("../../logger"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),ElasticsearchTransport=function(r){function e(e){var t=e.url,s=e.highWaterMark,a=void 0===s?500:s,i=e.consumeNum,o=void 0===i?20:i,u=tslib_1.__rest(e,["url","highWaterMark","consumeNum"]),n=r.call(this,tslib_1.__assign(tslib_1.__assign({},u),{highWaterMark:a,consumeNum:o,name:"ElasticsearchTransport",batchConsume:!0,forceDiscard:!0}))||this;return n.url=t,n}return tslib_1.__extends(e,r),e.prototype.logBatch=function(r,e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,s;return tslib_1.__generator(this,(function(a){switch(a.label){case 0:t=r.map((function(r){return r.message})),a.label=1;case 1:return a.trys.push([1,3,,4]),[4,this.request(t)];case 2:return a.sent(),e(),[3,4];case 3:return s=a.sent(),e(s),[3,4];case 4:return[2]}}))}))},e.prototype.request=function(r){return qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.request({url:this.url,data:r,method:"POST"})},e}(logger_1.LoggerTransport);exports.ElasticsearchTransport=ElasticsearchTransport; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ElasticsearchTransport=void 0;var tslib_1=require("tslib"),logger_1=require("../../logger"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),ElasticsearchTransport=function(e){function r(r){var t=r.url,s=r.skipUpload,i=r.highWaterMark,a=void 0===i?500:i,o=r.consumeNum,l=void 0===o?20:o,n=r.level,u=void 0===n?"info":n,c=tslib_1.__rest(r,["url","skipUpload","highWaterMark","consumeNum","level"]),p=e.call(this,tslib_1.__assign(tslib_1.__assign({},c),{highWaterMark:a,consumeNum:l,level:u,name:"ElasticsearchTransport",batchConsume:!0,forceDiscard:!0}))||this;return p.url=t,p.skipUpload=s,p}return tslib_1.__extends(r,e),r.prototype.logBatch=function(e,r){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,s;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:if(this.skipUpload)return r(),[2];t=e.map((function(e){return e.message})),i.label=1;case 1:return i.trys.push([1,3,,4]),[4,this.request(t)];case 2:return i.sent(),r(),[3,4];case 3:return s=i.sent(),r(s),[3,4];case 4:return[2]}}))}))},r.prototype.request=function(e){return qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.request({url:this.url,data:e,method:"POST"})},r}(logger_1.LoggerTransport);exports.ElasticsearchTransport=ElasticsearchTransport; | ||
//# sourceMappingURL=Elasticsearch.js.map |
@@ -5,2 +5,2 @@ import { Logger, LoggerOptions } from './Logger'; | ||
export * from './Logger'; | ||
export declare const createLogger: (opts: LoggerOptions) => Logger; | ||
export declare const createLogger: (opts?: LoggerOptions) => Logger; |
import { Format } from './Format'; | ||
export default function combine(...args: Format[]): Format; | ||
export declare function combine(...args: Format[]): Format; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tslib_1=require("tslib"),error_1=tslib_1.__importDefault(require("../../lib/error")),Format_1=require("./Format"),ERR_INVALID_FORMAT_INSTANCE=error_1.default.ERR_INVALID_FORMAT_INSTANCE,isValidFormat=function(r){if(!(r instanceof Format_1.Format))throw new ERR_INVALID_FORMAT_INSTANCE},cascade=function(r){if(r.every(isValidFormat))return function(e){var t,o,a=e;try{for(var i=tslib_1.__values(r),n=i.next();!n.done;n=i.next()){if(!(a=n.value.transform(a)))return!1}}catch(r){t={error:r}}finally{try{n&&!n.done&&(o=i.return)&&o.call(i)}finally{if(t)throw t.error}}return a}};function combine(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var t=Format_1.format(cascade(r));return t()}exports.default=combine; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.combine=void 0;var tslib_1=require("tslib"),error_1=tslib_1.__importDefault(require("../../lib/error")),Format_1=require("./Format"),ERR_INVALID_FORMAT_INSTANCE=error_1.default.ERR_INVALID_FORMAT_INSTANCE,isValidFormat=function(r){if(!(r instanceof Format_1.Format))throw new ERR_INVALID_FORMAT_INSTANCE},cascade=function(r){if(r.every(isValidFormat))return function(e){var t,o,i=e;try{for(var a=tslib_1.__values(r),n=a.next();!n.done;n=a.next()){if(!(i=n.value.transform(i)))return!1}}catch(r){t={error:r}}finally{try{n&&!n.done&&(o=a.return)&&o.call(a)}finally{if(t)throw t.error}}return i}};function combine(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var t=Format_1.format(cascade(r));return t()}exports.combine=combine; | ||
//# sourceMappingURL=combine.js.map |
@@ -1,2 +0,2 @@ | ||
import combine from './combine'; | ||
import { combine } from './combine'; | ||
import { LogInfo } from '../Logger'; | ||
@@ -3,0 +3,0 @@ declare type FormatFnOptions = Record<string, unknown>; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.format=exports.Format=void 0;var tslib_1=require("tslib"),combine_1=tslib_1.__importDefault(require("./combine")),Format=function(t){this.opts=t};exports.Format=Format,exports.format=function(t){var r=function(r){function e(){return null!==r&&r.apply(this,arguments)||this}return tslib_1.__extends(e,r),e.prototype.transform=function(r){return t(r,this.opts)},e}(Format);return function(t){return new r(t)}},exports.format.combine=combine_1.default; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.format=exports.Format=void 0;var tslib_1=require("tslib"),combine_1=require("./combine"),Format=function(t){this.opts=t};exports.Format=Format,exports.format=function(t){var r=function(r){function o(){return null!==r&&r.apply(this,arguments)||this}return tslib_1.__extends(o,r),o.prototype.transform=function(r){return t(r,this.opts)},o}(Format);return function(t){return new r(t)}},exports.format.combine=combine_1.combine; | ||
//# sourceMappingURL=Format.js.map |
{ | ||
"name": "qcloud-iotexplorer-logger", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "", | ||
@@ -25,3 +25,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "eb28008ba5b0849116447c397b68f6ff19262296" | ||
"gitHead": "9964c3f041ce86ed67190445ef57f18eeca01f30" | ||
} |
@@ -76,2 +76,44 @@ import errorCodes from './lib/error'; | ||
/** | ||
* @description 格式化打印的log | ||
* insightLogger打log规定,有3种情况 | ||
* 1、第一个参数是event(填充到上报data的event字段),第二个是对象(上报data的其他参数) | ||
* e.g. insightLogger.info('CGI', { data: requestData, message: 'success' }) | ||
* 2、第一个参数是event(填充到上报data的event字段),第二个参数是字符串(填充到上报data的message字段) | ||
* e.g. insightLogger.info('CGI', 'success') | ||
* 3、第一个参数是上报对象 | ||
* e.g. insightLogger.info({ event: 'CGI', message: 'success' }) | ||
* */ | ||
const formatLog = (curLog: LogInfo): UnknownRecord => { | ||
const [other] = curLog.splat; | ||
let curReportData: UnknownRecord; | ||
if (isPlainObject(curLog.message)) { | ||
curReportData = curLog.message; | ||
} else { | ||
curReportData = {}; | ||
if (typeof other === 'string') { | ||
curReportData.message = other; | ||
} else if (isPlainObject(other)) { | ||
Object.assign(curReportData, other); | ||
} | ||
curReportData.event = String(curLog.message); | ||
} | ||
curReportData.level = LogLevel[curLog.level]; | ||
curReportData.at = +new Date(); | ||
// 如果当前的level是error,并且error字段的值是类Error的实例,需要将message以及stack提取出来,否则JSON.stringify后是一个空对象 | ||
if (curReportData.level === LogLevel.error && curReportData.error instanceof Error) { | ||
curReportData.error = { | ||
message: curReportData.error.message, | ||
stack: curReportData.error.stack, | ||
}; | ||
} | ||
return curReportData; | ||
}; | ||
export const createInsightLogger = ({ | ||
@@ -102,34 +144,2 @@ level, | ||
class InsightLoggerFactory extends Indirect { | ||
/** | ||
* @description 格式化打印的log | ||
* insightLogger打log规定,有3种情况 | ||
* 1、第一个参数是event(填充到上报data的event字段),第二个是对象(上报data的其他参数) | ||
* e.g. insightLogger.info('CGI', { data: requestData, message: 'success' }) | ||
* 2、第一个参数是event(填充到上报data的event字段),第二个参数是字符串(填充到上报data的message字段) | ||
* e.g. insightLogger.info('CGI', 'success') | ||
* 3、第一个参数是上报对象 | ||
* e.g. insightLogger.info({ event: 'CGI', message: 'success' }) | ||
* */ | ||
private static formatLog(curLog: LogInfo) { | ||
const [other] = curLog.splat; | ||
let curReportData: UnknownRecord; | ||
if (isPlainObject(curLog.message)) { | ||
curReportData = curLog.message; | ||
} else { | ||
curReportData = {}; | ||
curReportData.event = String(curLog.message); | ||
if (typeof other === 'string') { | ||
curReportData.message = other; | ||
} else if (isPlainObject(other)) { | ||
Object.assign(curReportData, other); | ||
} | ||
} | ||
curReportData.level = LogLevel[curLog.level]; | ||
return curReportData; | ||
} | ||
parent: InsightLoggerFactory; | ||
@@ -187,3 +197,3 @@ protected derivedLevel: InsightLoggerOptions['derivedLevel']; | ||
const fn = format((info) => { | ||
let reportData = InsightLoggerFactory.formatLog(info); | ||
let reportData = formatLog(info); | ||
@@ -190,0 +200,0 @@ reportData = this.mergeLogInfo(reportData); |
@@ -7,5 +7,7 @@ import { LoggerTransport, LoggerTransportOptions } from '../../logger'; | ||
url: string; | ||
skipUpload?: boolean; | ||
} | ||
export class ElasticsearchTransport extends LoggerTransport { | ||
skipUpload: boolean; | ||
private readonly url: string; | ||
@@ -15,4 +17,6 @@ | ||
url, | ||
skipUpload, | ||
highWaterMark = 500, | ||
consumeNum = 20, | ||
level = 'info', | ||
...opts | ||
@@ -24,2 +28,3 @@ }: ElasticsearchTransportOptions) { | ||
consumeNum, | ||
level, | ||
name: 'ElasticsearchTransport', | ||
@@ -31,5 +36,11 @@ batchConsume: true, | ||
this.url = url; | ||
this.skipUpload = skipUpload; | ||
} | ||
protected async logBatch(logs, cb) { | ||
if (this.skipUpload) { | ||
cb(); | ||
return; | ||
} | ||
const uploadData = logs.map(item => item.message); | ||
@@ -36,0 +47,0 @@ |
@@ -7,2 +7,2 @@ import { Logger, LoggerOptions } from './Logger'; | ||
export const createLogger = (opts: LoggerOptions) => new Logger(opts); | ||
export const createLogger = (opts?: LoggerOptions) => new Logger(opts); |
@@ -34,3 +34,3 @@ import errorCodes from '../../lib/error'; | ||
export default function combine(...args: Format[]) { | ||
export function combine(...args: Format[]) { | ||
const combinedFormat = format(cascade(args)); | ||
@@ -37,0 +37,0 @@ |
@@ -1,2 +0,2 @@ | ||
import combine from './combine'; | ||
import { combine } from './combine'; | ||
import { LogInfo } from '../Logger'; | ||
@@ -3,0 +3,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82953
1288