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

Feat: add logging #21

Merged
merged 38 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fe0187d
add logging
KirillDogadin-std Mar 16, 2023
cc3845a
capitalize msg in logger
KirillDogadin-std Mar 16, 2023
561445e
prisma db logging
KirillDogadin-std Mar 16, 2023
1fabb55
adjust check for proper db log levels
KirillDogadin-std Mar 16, 2023
0277a0b
adjust readme with env var
KirillDogadin-std Mar 16, 2023
af86c86
log responses
KirillDogadin-std Mar 16, 2023
118c055
async
KirillDogadin-std Mar 16, 2023
d49b8bd
filter functionality
KirillDogadin-std Mar 16, 2023
1b63827
improve md files
KirillDogadin-std Mar 16, 2023
9d0442e
improve logging filter
KirillDogadin-std Mar 16, 2023
856ed6f
doc update
KirillDogadin-std Mar 16, 2023
9a4fe25
split code
KirillDogadin-std Mar 20, 2023
367532e
refactor logging
KirillDogadin-std Mar 21, 2023
12ca9d6
allow no args for child logger
KirillDogadin-std Mar 21, 2023
8bc37ee
integrate logger into apollo
KirillDogadin-std Mar 21, 2023
deeb411
unused deps
KirillDogadin-std Mar 21, 2023
0b97e87
readme adjust
KirillDogadin-std Mar 21, 2023
df042dc
debug command
KirillDogadin-std Mar 21, 2023
d41337b
type package
KirillDogadin-std Mar 21, 2023
3d484ec
lint
KirillDogadin-std Mar 21, 2023
e0a0352
lint
KirillDogadin-std Mar 21, 2023
690bd91
lint
KirillDogadin-std Mar 21, 2023
9c40e07
nits
KirillDogadin-std Mar 22, 2023
d33fb4a
Merge branch 'main' into add_logging
KirillDogadin-std Mar 22, 2023
3aa9dae
merge
KirillDogadin-std Mar 22, 2023
157f413
lint
KirillDogadin-std Mar 22, 2023
5c6a3ca
typecheck
KirillDogadin-std Mar 22, 2023
e2daa00
minor
KirillDogadin-std Mar 23, 2023
48c3264
lint
KirillDogadin-std Mar 23, 2023
2562aad
update readme
KirillDogadin-std Mar 23, 2023
e2d8fc0
make prisma log things via pino
KirillDogadin-std Mar 23, 2023
4e205e7
adjust types
KirillDogadin-std Mar 23, 2023
4caa41c
readme and minors
KirillDogadin-std Mar 23, 2023
e9fc986
Merge branch 'main' into add_logging
KirillDogadin-std Mar 27, 2023
76e73ed
tests added
KirillDogadin-std Mar 27, 2023
6cee018
added the comment with doc link
KirillDogadin-std Mar 27, 2023
58850a4
rm block for ending api
KirillDogadin-std Mar 28, 2023
47d6fdd
adjust tests
KirillDogadin-std Mar 28, 2023
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
1 change: 1 addition & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getUserCrud } from './modules';

const dbLogger = getChildLogger({ msgPrefix: 'DATABASE' });

// https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/logging#log-to-stdout
const LOGGING_EVENTS: { level: Prisma.LogLevel; emit: 'event' }[] = [
{
emit: 'event',
valiafetisov marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
17 changes: 0 additions & 17 deletions src/env.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this the reason why main was broken? Can you please be vocal about it? If you're fixing it in this PR (instead of opening a dedicated one) can you please add a build step to the CI to prevent this from happening in the future? 🙂

Copy link
Contributor Author

@KirillDogadin-std KirillDogadin-std Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this the reason why main was broken?

I did not notice that it was broken, what was the error / did not work?
i've ran it before writing this, seems to work on my machine

If you're fixing it in this PR

at least not via this file since it's not listed in the PR's diff.

can you please add a build step to the CI

what are we building exactly? i'm only familiar with builds of webapps where we want to be able to serve statically as a result of the command. But this here is an api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Via verbal discussion:

  • removed "api execution ended" message because now we have functionality in place so starting a server runs this api forever.

Reason for the message to appear at the first place:

  • startServer started to return a value (server) instead of spawning server and returning void (this change was needed for the test context init) which resulted into triggering then immediately.

This file was deleted.

4 changes: 1 addition & 3 deletions tests/coreunit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import builder from 'gql-query-builder';
import { CoreUnit } from '@prisma/client';
import { cleanDatabase as cleanDatabaseBeforeAfterEachTest } from './helpers/database';
import { executeGraphQlQuery } from './helpers/server';
import { getPrisma } from '../src/database';
import prisma from '../src/database';

cleanDatabaseBeforeAfterEachTest();

test('Core Unit: get all', async () => {
const prisma = getPrisma();
await prisma.coreUnit.create({
data: {
code: 'asdf',
Expand All @@ -32,7 +31,6 @@ test('Core Unit: get all', async () => {
});

test('Core Unit: get by id', async () => {
const prisma = getPrisma();
const created = await prisma.coreUnit.create({
data: {
code: 'asdf',
Expand Down
53 changes: 53 additions & 0 deletions tests/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
test, expect, vi, describe,
} from 'vitest';
import { getChildLogger } from '../src/logger';

function getValueBySymbolKey(obj: any, symbolStr: string) {
const key = Object.getOwnPropertySymbols(obj).filter(
(symbol) => symbol.toString() === symbolStr,
)[0];
return obj[key];
}

describe('Logger: metadata', () => {
test('Logger: creation, prefix and bindings', () => {
const logger = getChildLogger({ msgPrefix: 'test' }, { purpose: 'test' });
const msgPrefix: string = getValueBySymbolKey(
logger,
'Symbol(pino.msgPrefix)',
);
const bindings: string = getValueBySymbolKey(
logger,
'Symbol(pino.chindings)',
);
// value of bindings in the logger looks like `,"key":"value","key2":"value2"`
const bindingObject = JSON.parse(`{${bindings.slice(1)}}`);
expect(msgPrefix).toBe('[TEST] ');
expect(bindingObject.purpose).toBe('test');
valiafetisov marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('Logger: filters', () => {
vi.mock('../logger.config.ts', () => ({
loggerConfig: {
moduleFilter: ['tests/logger.test.ts'],
prefixFilter: ['testPrefix'],
logLevel: 'warn',
dbLogLevel: ['warn'],
httpLogLevel: 'debug',
},
}));

test('Logger: logging filter with both filters', () => {
const logger = getChildLogger({ msgPrefix: 'testPrefix' });
const level = getValueBySymbolKey(logger, 'Symbol(pino.levelVal)');
expect(level).toBe(40);
});

test('Logger: logging filter with module filter', () => {
const logger = getChildLogger(undefined, { module: 'testModule' });
const level = getValueBySymbolKey(logger, 'Symbol(pino.levelVal)');
expect(level).toBe(Infinity);
});
});