@logtail/core
Advanced tools
Comparing version 0.4.6 to 0.4.9
import { ILogLevel, ILogtailLog, ILogtailOptions, Context, Middleware, Sync } from "@logtail/types"; | ||
declare type Message = string | Error; | ||
type Message = string | Error; | ||
/** | ||
@@ -4,0 +4,0 @@ * Logtail core class for logging to the Logtail.com service |
@@ -68,3 +68,3 @@ "use strict"; | ||
// Create a throttler, for sync operations | ||
const throttle = tools_1.makeThrottle(this._options.syncMax); | ||
const throttle = (0, tools_1.makeThrottle)(this._options.syncMax); | ||
// Sync after throttling | ||
@@ -75,6 +75,6 @@ const throttler = throttle((logs) => { | ||
// Burst protection for logging | ||
this._logBurstProtection = tools_1.makeBurstProtection(this._options.burstProtectionMilliseconds, this._options.burstProtectionMax, 'Logging'); | ||
this._logBurstProtection = (0, tools_1.makeBurstProtection)(this._options.burstProtectionMilliseconds, this._options.burstProtectionMax, "Logging"); | ||
this.log = this._logBurstProtection(this.log.bind(this)); | ||
// Create a batcher, for aggregating logs by buffer size/interval | ||
const batcher = tools_1.makeBatch(this._options.batchSize, this._options.batchInterval, this._options.retryCount, this._options.retryBackoff); | ||
const batcher = (0, tools_1.makeBatch)(this._options.batchSize, this._options.batchInterval, this._options.retryCount, this._options.retryBackoff); | ||
this._batch = batcher.initPusher((logs) => { | ||
@@ -88,3 +88,3 @@ return throttler(logs); | ||
return { | ||
stack: e.stack | ||
stack: e.stack, | ||
}; | ||
@@ -133,8 +133,19 @@ } | ||
if (this._options.sendLogsToConsoleOutput) { | ||
if (["debug", "info", "warn", "error"].indexOf(level) !== -1) { | ||
console[level](message, context); | ||
switch (level) { | ||
case "debug": | ||
console.debug(message, context); | ||
break; | ||
case "info": | ||
console.info(message, context); | ||
break; | ||
case "warn": | ||
console.warn(message, context); | ||
break; | ||
case "error": | ||
console.error(message, context); | ||
break; | ||
default: | ||
console.log(`[${level.toUpperCase()}]`, message, context); | ||
break; | ||
} | ||
else { | ||
console.log(`[${level.toUpperCase()}]`, message, context); | ||
} | ||
} | ||
@@ -280,3 +291,2 @@ // Check that we have a sync function | ||
exports.default = default_1; | ||
; | ||
//# sourceMappingURL=base.js.map |
@@ -198,3 +198,6 @@ "use strict"; | ||
const e = new Error("Should NOT be ignored!"); | ||
const base = new base_1.default("testing", { ignoreExceptions: false, throwExceptions: true }); | ||
const base = new base_1.default("testing", { | ||
ignoreExceptions: false, | ||
throwExceptions: true, | ||
}); | ||
// Add a mock sync method which throws an error | ||
@@ -210,3 +213,3 @@ base.setSync(async () => { | ||
const base = new base_1.default("testing", { | ||
ignoreExceptions: true | ||
ignoreExceptions: true, | ||
}); | ||
@@ -250,3 +253,3 @@ // Add a mock sync method which throws an error | ||
const base = new base_1.default("testing", { | ||
sendLogsToBetterStack: false | ||
sendLogsToBetterStack: false, | ||
}); | ||
@@ -340,3 +343,6 @@ // Add a mock sync method which counts sync calls | ||
const message = "Testing logging"; | ||
const base = new base_1.default("testing", { burstProtectionMilliseconds: 100, burstProtectionMax: 100 }); | ||
const base = new base_1.default("testing", { | ||
burstProtectionMilliseconds: 100, | ||
burstProtectionMax: 100, | ||
}); | ||
// Add a mock sync method | ||
@@ -364,3 +370,6 @@ base.setSync(async (logs) => logs); | ||
const message = "Testing logging"; | ||
const base = new base_1.default("testing", { burstProtectionMilliseconds: 100, burstProtectionMax: 50 }); | ||
const base = new base_1.default("testing", { | ||
burstProtectionMilliseconds: 100, | ||
burstProtectionMax: 50, | ||
}); | ||
// Add a mock sync method | ||
@@ -375,3 +384,5 @@ base.setSync(async (logs) => logs); | ||
// Send logs with 1ms delay between them | ||
logs.push(new Promise(resolve => { setTimeout(() => base.info(message).then(resolve), i); })); | ||
logs.push(new Promise(resolve => { | ||
setTimeout(() => base.info(message).then(resolve), i); | ||
})); | ||
} | ||
@@ -378,0 +389,0 @@ await Promise.all(logs); |
@@ -6,2 +6,2 @@ import Base from "./base"; | ||
*/ | ||
Base }; | ||
Base, }; |
import { ILogLevel, ILogtailLog, ILogtailOptions, Context, Middleware, Sync } from "@logtail/types"; | ||
declare type Message = string | Error; | ||
type Message = string | Error; | ||
/** | ||
@@ -4,0 +4,0 @@ * Logtail core class for logging to the Logtail.com service |
@@ -1,2 +0,2 @@ | ||
import { LogLevel } from "@logtail/types"; | ||
import { LogLevel, } from "@logtail/types"; | ||
import { makeBatch, makeBurstProtection, makeThrottle } from "@logtail/tools"; | ||
@@ -72,3 +72,3 @@ // Set default options for Logtail | ||
// Burst protection for logging | ||
this._logBurstProtection = makeBurstProtection(this._options.burstProtectionMilliseconds, this._options.burstProtectionMax, 'Logging'); | ||
this._logBurstProtection = makeBurstProtection(this._options.burstProtectionMilliseconds, this._options.burstProtectionMax, "Logging"); | ||
this.log = this._logBurstProtection(this.log.bind(this)); | ||
@@ -85,3 +85,3 @@ // Create a batcher, for aggregating logs by buffer size/interval | ||
return { | ||
stack: e.stack | ||
stack: e.stack, | ||
}; | ||
@@ -130,8 +130,19 @@ } | ||
if (this._options.sendLogsToConsoleOutput) { | ||
if (["debug", "info", "warn", "error"].indexOf(level) !== -1) { | ||
console[level](message, context); | ||
switch (level) { | ||
case "debug": | ||
console.debug(message, context); | ||
break; | ||
case "info": | ||
console.info(message, context); | ||
break; | ||
case "warn": | ||
console.warn(message, context); | ||
break; | ||
case "error": | ||
console.error(message, context); | ||
break; | ||
default: | ||
console.log(`[${level.toUpperCase()}]`, message, context); | ||
break; | ||
} | ||
else { | ||
console.log(`[${level.toUpperCase()}]`, message, context); | ||
} | ||
} | ||
@@ -276,3 +287,2 @@ // Check that we have a sync function | ||
} | ||
; | ||
//# sourceMappingURL=base.js.map |
@@ -193,3 +193,6 @@ import Base from "./base"; | ||
const e = new Error("Should NOT be ignored!"); | ||
const base = new Base("testing", { ignoreExceptions: false, throwExceptions: true }); | ||
const base = new Base("testing", { | ||
ignoreExceptions: false, | ||
throwExceptions: true, | ||
}); | ||
// Add a mock sync method which throws an error | ||
@@ -205,3 +208,3 @@ base.setSync(async () => { | ||
const base = new Base("testing", { | ||
ignoreExceptions: true | ||
ignoreExceptions: true, | ||
}); | ||
@@ -245,3 +248,3 @@ // Add a mock sync method which throws an error | ||
const base = new Base("testing", { | ||
sendLogsToBetterStack: false | ||
sendLogsToBetterStack: false, | ||
}); | ||
@@ -335,3 +338,6 @@ // Add a mock sync method which counts sync calls | ||
const message = "Testing logging"; | ||
const base = new Base("testing", { burstProtectionMilliseconds: 100, burstProtectionMax: 100 }); | ||
const base = new Base("testing", { | ||
burstProtectionMilliseconds: 100, | ||
burstProtectionMax: 100, | ||
}); | ||
// Add a mock sync method | ||
@@ -359,3 +365,6 @@ base.setSync(async (logs) => logs); | ||
const message = "Testing logging"; | ||
const base = new Base("testing", { burstProtectionMilliseconds: 100, burstProtectionMax: 50 }); | ||
const base = new Base("testing", { | ||
burstProtectionMilliseconds: 100, | ||
burstProtectionMax: 50, | ||
}); | ||
// Add a mock sync method | ||
@@ -370,3 +379,5 @@ base.setSync(async (logs) => logs); | ||
// Send logs with 1ms delay between them | ||
logs.push(new Promise(resolve => { setTimeout(() => base.info(message).then(resolve), i); })); | ||
logs.push(new Promise(resolve => { | ||
setTimeout(() => base.info(message).then(resolve), i); | ||
})); | ||
} | ||
@@ -373,0 +384,0 @@ await Promise.all(logs); |
@@ -6,2 +6,2 @@ import Base from "./base"; | ||
*/ | ||
Base }; | ||
Base, }; |
@@ -6,3 +6,3 @@ import Base from "./base"; | ||
*/ | ||
Base }; | ||
Base, }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@logtail/core", | ||
"version": "0.4.6", | ||
"version": "0.4.9", | ||
"description": "Logtail.com - logging core", | ||
@@ -40,9 +40,9 @@ "keywords": [ | ||
"npm-run-all": "^4.1.5", | ||
"typescript": "^3.9.7" | ||
"typescript": "^4.9.5" | ||
}, | ||
"dependencies": { | ||
"@logtail/tools": "^0.4.6", | ||
"@logtail/types": "^0.4.6" | ||
"@logtail/tools": "^0.4.9", | ||
"@logtail/types": "^0.4.9" | ||
}, | ||
"gitHead": "7c2faf339a1ea766cd986896739ee4c47788a06e" | ||
"gitHead": "47b6c44046d9d9d7789f385381afbf33d9431d57" | ||
} |
@@ -30,6 +30,3 @@ # [Better Stack](https://betterstack.com/logs) JavaScript client: Logging core library | ||
// Constructor must take a Logtail.com source token, and (optional) options | ||
public constructor( | ||
sourceToken: string, | ||
options?: Partial<ILogtailOptions> | ||
) { | ||
public constructor(sourceToken: string, options?: Partial<ILogtailOptions>) { | ||
// Make sure you pass the source token to the parent constructor! | ||
@@ -84,3 +81,3 @@ super(sourceToken, options); | ||
// Will resolve when synced with Logtail.com (or reject if there's an error) | ||
logtail.log("some log message").then((log) => { | ||
logtail.log("some log message").then(log => { | ||
// `log` is the transformed log, after going through middleware | ||
@@ -87,0 +84,0 @@ }); |
@@ -118,3 +118,3 @@ import Base from "./base"; | ||
// Trigger flush | ||
await base.flush() | ||
await base.flush(); | ||
@@ -140,3 +140,3 @@ // After flush, synced should be now be 1 | ||
...log, | ||
message: newMessage | ||
message: newMessage, | ||
}; | ||
@@ -269,3 +269,6 @@ }); | ||
const e = new Error("Should NOT be ignored!"); | ||
const base = new Base("testing", { ignoreExceptions: false, throwExceptions: true }); | ||
const base = new Base("testing", { | ||
ignoreExceptions: false, | ||
throwExceptions: true, | ||
}); | ||
@@ -284,3 +287,3 @@ // Add a mock sync method which throws an error | ||
const base = new Base("testing", { | ||
ignoreExceptions: true | ||
ignoreExceptions: true, | ||
}); | ||
@@ -308,3 +311,3 @@ | ||
let logsCount = 0; | ||
base.setSync(async (logs) => { | ||
base.setSync(async logs => { | ||
syncCount++; | ||
@@ -333,8 +336,8 @@ logsCount = logs.length; | ||
const base = new Base("testing", { | ||
sendLogsToBetterStack: false | ||
sendLogsToBetterStack: false, | ||
}); | ||
// Add a mock sync method which counts sync calls | ||
let syncCount = 0 | ||
base.setSync(async (log) => { | ||
let syncCount = 0; | ||
base.setSync(async log => { | ||
syncCount++; | ||
@@ -372,4 +375,4 @@ return log; | ||
// Mock console methods | ||
const originalConsole = console | ||
const consoleOutputs:any = [] | ||
const originalConsole = console; | ||
const consoleOutputs: any = []; | ||
console = { | ||
@@ -382,3 +385,3 @@ ...console, | ||
log: (...args: any) => consoleOutputs.push(["log", ...args]), | ||
} | ||
}; | ||
@@ -410,3 +413,3 @@ await base.debug(message); | ||
console = originalConsole | ||
console = originalConsole; | ||
}); | ||
@@ -444,3 +447,5 @@ | ||
expect(mockedConsoleError).toHaveBeenCalledWith("Logging was called more than 10000 times during last 5000ms. Ignoring."); | ||
expect(mockedConsoleError).toHaveBeenCalledWith( | ||
"Logging was called more than 10000 times during last 5000ms. Ignoring.", | ||
); | ||
expect(mockedConsoleError).toHaveBeenCalledTimes(1); | ||
@@ -452,3 +457,6 @@ }); | ||
const message = "Testing logging"; | ||
const base = new Base("testing", { burstProtectionMilliseconds: 100, burstProtectionMax: 100}); | ||
const base = new Base("testing", { | ||
burstProtectionMilliseconds: 100, | ||
burstProtectionMax: 100, | ||
}); | ||
@@ -484,3 +492,6 @@ // Add a mock sync method | ||
const message = "Testing logging"; | ||
const base = new Base("testing", { burstProtectionMilliseconds: 100, burstProtectionMax: 50 }); | ||
const base = new Base("testing", { | ||
burstProtectionMilliseconds: 100, | ||
burstProtectionMax: 50, | ||
}); | ||
@@ -498,3 +509,7 @@ // Add a mock sync method | ||
// Send logs with 1ms delay between them | ||
logs.push(new Promise(resolve => { setTimeout(() => base.info(message).then(resolve), i) })) | ||
logs.push( | ||
new Promise(resolve => { | ||
setTimeout(() => base.info(message).then(resolve), i); | ||
}), | ||
); | ||
} | ||
@@ -512,3 +527,5 @@ | ||
expect(mockedConsoleError).toHaveBeenCalledWith("Logging was called more than 50 times during last 100ms. Ignoring."); | ||
expect(mockedConsoleError).toHaveBeenCalledWith( | ||
"Logging was called more than 50 times during last 100ms. Ignoring.", | ||
); | ||
expect(mockedConsoleError).toHaveBeenCalledTimes(5); | ||
@@ -515,0 +532,0 @@ }); |
@@ -8,3 +8,3 @@ import { | ||
Middleware, | ||
Sync | ||
Sync, | ||
} from "@logtail/types"; | ||
@@ -107,6 +107,3 @@ import { makeBatch, makeBurstProtection, makeThrottle } from "@logtail/tools"; | ||
*/ | ||
public constructor( | ||
sourceToken: string, | ||
options?: Partial<ILogtailOptions> | ||
) { | ||
public constructor(sourceToken: string, options?: Partial<ILogtailOptions>) { | ||
// First, check we have a valid source token | ||
@@ -133,7 +130,7 @@ if (typeof sourceToken !== "string" || sourceToken === "") { | ||
this._logBurstProtection = makeBurstProtection( | ||
this._options.burstProtectionMilliseconds, | ||
this._options.burstProtectionMax, | ||
'Logging', | ||
) | ||
this.log = this._logBurstProtection(this.log.bind(this)) | ||
this._options.burstProtectionMilliseconds, | ||
this._options.burstProtectionMax, | ||
"Logging", | ||
); | ||
this.log = this._logBurstProtection(this.log.bind(this)); | ||
@@ -145,3 +142,3 @@ // Create a batcher, for aggregating logs by buffer size/interval | ||
this._options.retryCount, | ||
this._options.retryBackoff | ||
this._options.retryBackoff, | ||
); | ||
@@ -159,3 +156,3 @@ | ||
return { | ||
stack: e.stack | ||
stack: e.stack, | ||
}; | ||
@@ -170,3 +167,3 @@ } | ||
public async flush() { | ||
return this._flush() | ||
return this._flush(); | ||
} | ||
@@ -212,9 +209,21 @@ | ||
level: ILogLevel = LogLevel.Info, | ||
context: TContext = {} as TContext | ||
context: TContext = {} as TContext, | ||
): Promise<ILogtailLog & TContext> { | ||
if (this._options.sendLogsToConsoleOutput) { | ||
if (["debug", "info", "warn", "error"].indexOf(level) !== -1) { | ||
console[level as keyof typeof console](message, context) | ||
} else { | ||
console.log(`[${level.toUpperCase()}]`, message, context) | ||
switch (level) { | ||
case "debug": | ||
console.debug(message, context); | ||
break; | ||
case "info": | ||
console.info(message, context); | ||
break; | ||
case "warn": | ||
console.warn(message, context); | ||
break; | ||
case "error": | ||
console.error(message, context); | ||
break; | ||
default: | ||
console.log(`[${level.toUpperCase()}]`, message, context); | ||
break; | ||
} | ||
@@ -240,3 +249,3 @@ } | ||
// Add initial context | ||
...context | ||
...context, | ||
}; | ||
@@ -256,3 +265,3 @@ | ||
// Add error message | ||
message: message.message | ||
message: message.message, | ||
}; | ||
@@ -265,3 +274,3 @@ } else { | ||
// Add string message | ||
message | ||
message, | ||
}; | ||
@@ -320,3 +329,3 @@ } | ||
message: Message, | ||
context: TContext = {} as TContext | ||
context: TContext = {} as TContext, | ||
) { | ||
@@ -336,3 +345,3 @@ return this.log(message, LogLevel.Debug, context); | ||
message: Message, | ||
context: TContext = {} as TContext | ||
context: TContext = {} as TContext, | ||
) { | ||
@@ -352,3 +361,3 @@ return this.log(message, LogLevel.Info, context); | ||
message: Message, | ||
context: TContext = {} as TContext | ||
context: TContext = {} as TContext, | ||
) { | ||
@@ -368,3 +377,3 @@ return this.log(message, LogLevel.Warn, context); | ||
message: Message, | ||
context: TContext = {} as TContext | ||
context: TContext = {} as TContext, | ||
) { | ||
@@ -407,5 +416,9 @@ return this.log(message, LogLevel.Error, context); | ||
export default class extends Logtail { | ||
async log<TContext extends Context>(message: Message, level: ILogLevel = LogLevel.Info, context: TContext = {} as TContext): Promise<ILogtailLog & TContext> { | ||
async log<TContext extends Context>( | ||
message: Message, | ||
level: ILogLevel = LogLevel.Info, | ||
context: TContext = {} as TContext, | ||
): Promise<ILogtailLog & TContext> { | ||
return super.log(message, level, context); | ||
} | ||
}; | ||
} |
@@ -7,3 +7,3 @@ import Base from "./base"; | ||
*/ | ||
Base | ||
Base, | ||
}; |
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
137678
2430
141
Updated@logtail/tools@^0.4.9
Updated@logtail/types@^0.4.9