Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max listeners & 1.0.0 release #12

Merged
merged 2 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ module.exports = {
// TS
'@typescript-eslint/ban-ts-comment': 'off',
// ES:off + TS:on
'semi': 'off',
'@typescript-eslint/semi': ['error', 'never'],
'indent': 'off',
'@typescript-eslint/indent': ['error', 2],
'quotes': 'off',
'@typescript-eslint/quotes': [
'error', 'single', { 'allowTemplateLiterals': true },
],
'comma-dangle': 'off',
'@typescript-eslint/comma-dangle': ['error', 'always-multiline'],
// ES
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datalust/winston-seq",
"version": "0.1.2",
"version": "1.0.0",
"license": "Apache-2.0",
"description": "A Winston v3 transport for Seq",
"author": "Datalust and contributors",
Expand Down
93 changes: 67 additions & 26 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ describe('winston-seq', () => {
})

afterAll(() => {
if (!!logger) logger.close()
if (logger) logger.close()
})

it('should send a log to seq with defaultMeta properties', async () => {
const random = getRandom();
logger.debug(
"User {user} purchase product {product} at ${price}",
"User {user} purchase product {product} at ${price}",
{
user: "Millie Gilbert",
product: "Yardtime Garden Shears",
price: 29.99,
random
random,
});
await transport.flush();
const event = await queryEvent(`random = '${random}'`);
expect(getPropertyFromEvent(event, 'user')).toBe("Millie Gilbert");
expect(getPropertyFromEvent(event, 'application')).toBe("logtests");
expect(getPropertyFromEvent(event, 'product')).toBe("Yardtime Garden Shears");
expect(getPropertyFromEvent(event, 'price')).toBe(29.99);
expect(event.RenderedMessage).toBe('User Millie Gilbert purchase product Yardtime Garden Shears at $29.99');

expect(event.RenderedMessage).toBe(
'User Millie Gilbert purchase product Yardtime Garden Shears at $29.99');
})

it('should send with child logger context', async () => {
Expand All @@ -82,22 +82,58 @@ describe('winston-seq', () => {
expect(events.length).toBe(10);
})

it('should send events via many loggers', async () => {
// This test exists to investigate issue https://github.com/datalust/winston-seq/issues/8
// A warning is written if many loggers share a transport.
// This behaviour comes from winston. See https://github.com/winstonjs/winston/issues/1334
const random = getRandom();

const bulkLoggers = [...Array(5).keys()].map(() => {
const t = new SeqTransport({
serverUrl: process.env.SEQ_INGESTION_URL,
apiKey: process.env.SEQ_API_KEY,
onError: (e => { console.error(e) }),
handleExceptions: true,
handleRejections: true,
});
const l = winston.createLogger({
level: 'silly',
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.json(),
),
defaultMeta: { application: 'logtests' },
transports: [
t,
],
});
return { t, l };
});

bulkLoggers.forEach(({ t, l }) => {
for (let n = 1; n <= 10000; n++) {
l.info('Logging event number {n}', { n, random });
}
});
await Promise.all(bulkLoggers.map(({ t, l }) => t.flush()));
});

it('should allow explicit timestamp', async () => {
const random = getRandom();
const tenMinutesAgo = new Date(new Date().getTime() - 600 * 1000);
const threeMinutesAgo = new Date(new Date().getTime() - 180 * 1000);
logger.info("Logging at {now} with past timestamp {timestamp}", {
now: new Date(),
timestamp: tenMinutesAgo,
random
timestamp: threeMinutesAgo,
random,
});
await transport.flush();
const event = await queryEvent(`random = '${random}'`);
expect(event).toBeDefined();
expect(getPropertyFromEvent(event, 'timestamp')).toBe(tenMinutesAgo.toISOString());
expect(getPropertyFromEvent(event, 'timestamp')).toBe(threeMinutesAgo.toISOString());

});

it('should work with different formats', async ()=>{
it('should work with different formats', async () => {
const random = getRandom();
const diffFormatTransport = new SeqTransport({
serverUrl: process.env.SEQ_INGESTION_URL,
Expand All @@ -119,7 +155,9 @@ describe('winston-seq', () => {
],
});

diffFormatLogger.info("winston-seq: tests: should work with different {whats}", {whats: "formats", random});
diffFormatLogger.info(
"winston-seq: tests: should work with different {whats}",
{ whats: "formats", random });
diffFormatLogger.close();
await diffFormatTransport.flush();

Expand All @@ -128,7 +166,7 @@ describe('winston-seq', () => {
expect(getPropertyFromEvent(event, 'whats')).toBe('formats');
})

it('should work with no API key', async ()=>{
it('should work with no API key', async () => {
const random = getRandom();
const anonTransport = new SeqTransport({
serverUrl: process.env.SEQ_INGESTION_URL,
Expand All @@ -144,34 +182,37 @@ describe('winston-seq', () => {
],
});

anonLogger.info("winston-seq: tests: should work with no API key", {random});
anonLogger.info("winston-seq: tests: should work with no API key", { random });
anonLogger.close();
await anonTransport.flush();

const event = await queryEvent(`random = '${random}'`);
expect(event).toBeDefined();
})

it('should log exceptions', async ()=>{
it('should log exceptions', async () => {
const random = getRandom();
try{
throw new Error("Test error");
} catch (e) {
logger.error(e, {random});
logger.error(e, { random });
}
await transport.flush();
const event = await queryEvent(`random = '${random}'`);
expect(event).toBeDefined();
expect(event.Exception).toMatch(/Error: Test error(.|\W)+/);
});

it('should log all logging levels', async ()=>{
it('should log all logging levels', async () => {
const random = getRandom();
const levels = ['error', 'warn', 'info', 'http', 'verbose', 'debug', 'silly'];
await Promise.all(levels.map(level => {
logger.log(level, "winston-seq: testing: logging levels for level {level}", {level, random, test: 'should log all logging levels'});
return transport.flush();
}));
levels.map(level => {
logger.log(
level,
"winston-seq: testing: logging levels",
{ level, random, test: 'should log all logging levels' });
});
await transport.flush();
const events = await queryEvents(`random = '${random}'`);
expect(events).toBeDefined();
expect(events.some((event: any) => event.Level == 'error')).toBe(true);
Expand All @@ -184,27 +225,27 @@ describe('winston-seq', () => {
});
})

function getPropertyFromEvent(event: any, propertyName: string) {
function getPropertyFromEvent (event: any, propertyName: string) {
return event.Properties.find((p: any) => p.Name === propertyName).Value;
}

function getMessageFromEvent(event: any) {
function getMessageFromEvent (event: any) {
return event.MessageTemplateTokens[0].Text;
}

function getRandom() {
function getRandom () {
return Math.round(Math.random() * 1000000).toString();
}

async function queryEvent(filter: string) {
async function queryEvent (filter: string) {
const response = await axios.get(`${process.env.SEQ_API_URL}/api/events/signal?filter=${encodeURIComponent(filter)}&count=1&render=true&shortCircuitAfter=100&apiKey=${process.env.SEQ_API_KEY}`);
if (response.data.Events.length === 0) {
throw new Error('No events match filter ' + filter);
}
return response.data.Events[0];
}

async function queryEvents(filter: string) {
async function queryEvents (filter: string) {
const response = await axios.get(`${process.env.SEQ_API_URL}/api/events/signal?filter=${encodeURIComponent(filter)}&count=150&shortCircuitAfter=100&apiKey=${process.env.SEQ_API_KEY}`);
return response.data.Events;
}
}
Loading