Skip to content

Commit

Permalink
feat(infra): logger module (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil authored Mar 24, 2021
1 parent 7a54a73 commit 442a8f7
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ include:
file: /base_deploy_kosko_stage.yml
ref: v20.6.1
#
- /infra/logger/.gitlab-ci.yml
#
- /targets/alert-cli/.gitlab-ci.yml
- /targets/ingester/.gitlab-ci.yml
- /targets/hasura/.gitlab-ci.yml
Expand Down
2 changes: 2 additions & 0 deletions infra/logger/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
3 changes: 3 additions & 0 deletions infra/logger/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
extends:
- "@socialgouv/eslint-config-typescript"
2 changes: 2 additions & 0 deletions infra/logger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
82 changes: 82 additions & 0 deletions infra/logger/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#
#
#

Install logger:
extends: .autodevops_install
image: node:14.16.0-alpine3.11
cache:
key:
files:
- yarn.lock
prefix: ${CI_JOB_NAME}
paths:
- yarn
script:
- yarn config set cache-folder ${CI_PROJECT_DIR}/.cache/yarn
- yarn global add @socialgouv/yarn-workspace-focus-install
- yarn exec yarn-workspace-focus-install
--
--cwd infra/logger
--
--frozen-lockfile --prefer-offline
artifacts:
expire_in: 1 week
paths:
- infra/logger/node_modules
- node_modules

#
#
#

Build logger:
extends: .base_yarn_script
image: node:14.16.0-alpine3.11
needs:
- job: Install logger
artifacts: true
before_script:
- cd infra/logger
script:
- yarn build
artifacts:
expire_in: 1 week
paths:
- infra/logger/lib

#
#
#

Lint logger:
extends: .autodevops_lint
dependencies: null
image: node:14.16-alpine3.13
needs:
- job: Install logger
artifacts: true
before_script:
- cd infra/logger
script:
- yarn lint

#
#
#

Test logger:
extends: .autodevops_test
dependencies: null
image: node:14.16-alpine3.13
needs:
- job: Install logger
artifacts: true
before_script:
- cd infra/logger
script:
- yarn test
artifacts:
expire_in: 1 week
paths:
- infra/logger/lib
62 changes: 62 additions & 0 deletions infra/logger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@socialgouv/cdtn-logger",
"description": "SocialGouv - Code du travail numerique - Infrastructure - Logger",
"version": "1.0.0-alpha.1",
"babel": {
"env": {
"test": {
"presets": [
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-transform-modules-commonjs",
"babel-plugin-dynamic-import-node"
]
}
}
},
"dependencies": {
"winston": "^3.3.3"
},
"devDependencies": {
"@babel/core": "^7.12.17",
"@babel/plugin-transform-modules-commonjs": "^7.13.8",
"@babel/preset-typescript": "^7.13.0",
"@socialgouv/eslint-config-typescript": "^1.57.0",
"@tsconfig/node14": "^1.0.0",
"@types/jest": "^26.0.20",
"@types/std-mocks": "^1.0.0",
"babel-plugin-dynamic-import-node": "^2.3.3",
"eslint": "^7.19.0",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"std-mocks": "^1.0.1",
"typescript": "^4.1.5"
},
"license": "Apache-2.0",
"main": "lib/index.js",
"private": "true",
"repository": {
"directory": "infra/logger",
"type": "git",
"url": "https://github.com/SocialGouv/cdtn-admin.git"
},
"scripts": {
"build": "tsc -p tsconfig.build.json",
"lint": "eslint src",
"lint:fix": "yarn lint --fix",
"prepush": "yarn lint && yarn test",
"precommit": "lint-staged",
"test": "jest",
"watch": "yarn build --watch --preserveWatchOutput"
},
"lint-staged": {
"src/**": [
"yarn lint --fix",
"yarn test --bail --findRelatedTests"
]
},
"sideEffects": false,
"typings": "lib/index.d.ts"
}
78 changes: 78 additions & 0 deletions infra/logger/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//

import { flush, restore, use } from "std-mocks";

process.env.NODE_ENV = "production";

test("should log less than or equal to info level to stdout", async () => {
// NOTE(douglasduteil): remove any LOG_LEVEL set in the env
// Ensute that the external env does not interfer with the test.
delete process.env.LOG_LEVEL;

const { logger } = await import("./index");

use();

logger.error("an error");
logger.warn("an warn");
logger.info("an info");
logger.http("an http");
logger.verbose("an verbose");
logger.debug("an debug");
logger.silly("an debug");

restore();

const output = flush();
expect(output.stderr).toMatchInlineSnapshot(`Array []`);
expect(output.stdout).toMatchInlineSnapshot(`
Array [
"{\\"message\\":\\"an error\\",\\"level\\":\\"error\\"}
",
"{\\"message\\":\\"an warn\\",\\"level\\":\\"warn\\"}
",
"{\\"message\\":\\"an info\\",\\"level\\":\\"info\\"}
",
]
`);
});

test("should log all levels to stdout", async () => {
jest.resetModules();

process.env.LOG_LEVEL = "silly";
const { logger } = await import("./index");

use();

logger.error("an error");
logger.warn("an warn");
logger.info("an info");
logger.http("an http");
logger.verbose("an verbose");
logger.debug("an debug");
logger.silly("an debug");

restore();

const output = flush();
expect(output.stderr).toMatchInlineSnapshot(`Array []`);
expect(output.stdout).toMatchInlineSnapshot(`
Array [
"{\\"message\\":\\"an error\\",\\"level\\":\\"error\\"}
",
"{\\"message\\":\\"an warn\\",\\"level\\":\\"warn\\"}
",
"{\\"message\\":\\"an info\\",\\"level\\":\\"info\\"}
",
"{\\"message\\":\\"an http\\",\\"level\\":\\"http\\"}
",
"{\\"message\\":\\"an verbose\\",\\"level\\":\\"verbose\\"}
",
"{\\"message\\":\\"an debug\\",\\"level\\":\\"debug\\"}
",
"{\\"message\\":\\"an debug\\",\\"level\\":\\"silly\\"}
",
]
`);
});
25 changes: 25 additions & 0 deletions infra/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//

import { createLogger, format, transports } from "winston";

const { Console } = transports;

export const LOG_LEVEL = process.env.LOG_LEVEL ?? "info";
export const logger = createLogger({
level: LOG_LEVEL,
transports: [],
});

if (process.env.NODE_ENV !== "production") {
logger.add(
new Console({
format: format.combine(format.colorize(), format.simple()),
})
);
} else {
logger.add(
new Console({
format: format.json(),
})
);
}
5 changes: 5 additions & 0 deletions infra/logger/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"files": ["./src/index.ts"]
}
9 changes: 9 additions & 0 deletions infra/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "lib",
"rootDir": "src"
}
}
6 changes: 2 additions & 4 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"packages": [
"shared/*",
"targets/*"
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "1.0.0-alpha.1"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"k8s": "yarn --silent --cwd .k8s"
},
"workspaces": [
"infra/*",
"shared/*",
"targets/*"
],
Expand Down
Loading

0 comments on commit 442a8f7

Please sign in to comment.