Skip to content

Commit c5a694f

Browse files
authored
Merge pull request #7 from emartech/typescript
feat(typescript): transform codebase to Typescript
2 parents 04d3383 + b37aee4 commit c5a694f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+526
-337
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# Created by https://www.gitignore.io/api/osx,node,linux,intellij+all,visualstudiocode
33
package-lock.json
4+
dist
45
### Intellij+all ###
56
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
67
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
.idea
44
examples
5+
src

examples/express.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const express = require('express');
4-
const logFactory = require('../index');
4+
const logFactory = require('../dist');
55
const clsAdapter = require('@emartech/cls-adapter');
66
const logger = logFactory('example');
77
const port = 3000;

examples/index.js renamed to examples/index-js.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
process.env.DEBUG = 'redis';
4-
const logger = require('../index');
4+
const logger = require('../dist');
55

66
const mongoLogger = logger('mongo');
77
const redisLogger = logger('redis');

examples/index-ts.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
process.env.DEBUG = 'redis';
2+
import logger from '../dist';
3+
4+
const mongoLogger = logger('mongo');
5+
const redisLogger = logger('redis');
6+
7+
// simple info logging with enabled namespace
8+
redisLogger.info('connected', { domain: 'yahoo' });
9+
10+
// not enabled
11+
mongoLogger.info('connected', { domain: 'google' });
12+
13+
// error objects
14+
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
15+
16+
// displays as is
17+
console.log(JSON.stringify({ example: 'output' }));

examples/koa.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const Koa = require('koa');
4-
const logFactory = require('../index');
4+
const logFactory = require('../dist');
5+
56
const clsAdapter = require('@emartech/cls-adapter');
67
const logger = logFactory('example');
78
const port = 3000;

index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
"name": "@emartech/json-logger",
33
"version": "0.0.0",
44
"description": "Tiny and fast json logger with namespace support",
5-
"main": "index.js",
5+
"main": "dist/index.js",
66
"scripts": {
7-
"test": "mocha ./src/ --recursive",
8-
"test:watch": "mocha ./src/ --recursive --watch",
7+
"test": "mocha --require ts-node/register ./src/ --recursive",
8+
"test:watch": "mocha --require ts-node/register ./src/ --recursive --watch",
9+
"build": "rm -rf dist && tsc --project ./tsconfig.json",
910
"semantic-release": "CI=true semantic-release",
11+
"example-js": "DEBUG=* node examples/index-js.js",
12+
"example-ts": "DEBUG=* ts-node examples/index-ts.ts",
1013
"koa-example": "DEBUG=* node examples/koa.js",
1114
"express-example": "DEBUG=* node examples/express.js"
1215
},
@@ -29,13 +32,16 @@
2932
},
3033
"devDependencies": {
3134
"@emartech/cls-adapter": "1.3.0",
35+
"@types/node": "18.7.2",
3236
"chai": "4.3.6",
3337
"express": "4.18.1",
3438
"koa": "2.13.4",
3539
"mocha": "10.0.0",
3640
"semantic-release": "15.5.0",
3741
"sinon": "14.0.0",
38-
"sinon-chai": "3.7.0"
42+
"sinon-chai": "3.7.0",
43+
"ts-node": "10.9.1",
44+
"typescript": "4.7.4"
3945
},
4046
"repository": {
4147
"type": "git",

src/config.js renamed to src/config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
'use strict';
1+
import chalk from 'chalk';
22

3-
const chalk = require('chalk');
3+
interface Color {
4+
number: number;
5+
name: string;
6+
}
47

5-
const levels = {
8+
const levels: Record<string, Color> = {
69
trace: {
710
number: 10,
811
name: 'TRACE'
@@ -31,9 +34,9 @@ const levels = {
3134

3235
const availableLevels = Object.keys(levels);
3336

34-
const coloredNames = {};
37+
const coloredNames: Record<string, string> = {};
3538
availableLevels.forEach((levelName) => {
3639
coloredNames[levels[levelName].number] = levels[levelName].name;
3740
});
3841

39-
module.exports = { levels, availableLevels, coloredNames };
42+
export const config = { levels, availableLevels, coloredNames };

src/enabled/enabled.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const isNamespaceEnabled = require('./enabled');
3+
const { isNamespaceEnabled } = require('./enabled');
44

55
describe('isNamespaceAvailable', function() {
66
it('should enable when variables only contain one', function() {

src/enabled/enabled.js renamed to src/enabled/enabled.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
function isNamespaceEnabled(availableNamespaces, namespace) {
4-
availableNamespaces = availableNamespaces.split(/[\s,]+/);
1+
export function isNamespaceEnabled(availableNamespace: string, namespace: string) {
2+
const availableNamespaces = availableNamespace.split(/[\s,]+/);
53
let enabled = false;
6-
const adds = [];
7-
const skips = [];
4+
const adds: RegExp[] = [];
5+
const skips: RegExp[] = [];
86

97
availableNamespaces.forEach(function(name) {
108
if (!name) {
@@ -33,5 +31,3 @@ function isNamespaceEnabled(availableNamespaces, namespace) {
3331

3432
return enabled;
3533
}
36-
37-
module.exports = isNamespaceEnabled;

src/formatter/debug.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/formatter/debug.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const debug = require('./debug');
4-
const ColorName = require('../output/color-name/color-name');
3+
const { debugFormatter } = require('./debug');
4+
const { ColorName } = require('../output/color-name/color-name');
55

66
describe('debug formatter', function() {
77
afterEach(function () {
@@ -11,6 +11,6 @@ describe('debug formatter', function() {
1111
it('should format line', function() {
1212
const logLine = { level: 10, time: new Date().toISOString(), name: 'redis', random: 15 };
1313

14-
expect(debug(logLine)).to.eql('\u001b[36mredis\u001b[39m TRACE +0ms random=15');
14+
expect(debugFormatter(logLine)).to.eql('\u001b[36mredis\u001b[39m TRACE +0ms random=15');
1515
});
16-
});
16+
});

src/formatter/debug.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ColorName } from '../output/color-name/color-name';
2+
import { stringifyLevel } from '../output/stringify-level/stringify-level';
3+
import { FormatTime } from '../output/format-time/format-time';
4+
import { formatBody } from '../output/format-body/format-body';
5+
6+
interface Log {
7+
name: string;
8+
level: string;
9+
}
10+
11+
const formatTime = new FormatTime();
12+
export function debugFormatter(log: Log) {
13+
return [
14+
ColorName.addColor(log.name),
15+
stringifyLevel(log.level),
16+
formatTime.elapsedTime(),
17+
formatBody(log)
18+
].join(' ');
19+
};

src/formatter/index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/formatter/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { jsonFormatter } from './json';
2+
import { logentriesFormatter } from './logentries';
3+
import { debugFormatter } from './debug';
4+
5+
export const formatter = {
6+
json: jsonFormatter,
7+
debug: debugFormatter,
8+
logentries: logentriesFormatter
9+
};

src/formatter/json.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/formatter/json.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const jsonFormatter = require('./json');
3+
const { jsonFormatter } = require('./json');
44

55
describe('json formatter', function() {
66
it('should stringify object to json', function() {

src/formatter/json.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function jsonFormatter(log: unknown) {
2+
return JSON.stringify(log);
3+
}

src/formatter/logentries.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/formatter/logentries.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const logentriesFormatter = require('./logentries');
3+
const { logentriesFormatter } = require('./logentries');
44

55
describe('logentries formatter', function() {
66
it('should stringify single field to key value pairs', function() {

src/formatter/logentries.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const isNumeric = (value: unknown) => !isNaN(parseFloat(value as string)) && isFinite(value as number);
4+
5+
const isString = (value: unknown) => typeof value === 'string' || value instanceof String;
6+
7+
const convertToTag = (value: unknown, key: string) => {
8+
if (isString(value)) {
9+
value = JSON.stringify(value);
10+
} else if(isNumeric(value)) {
11+
value = (value as number).toString();
12+
} else {
13+
value = '"' + JSON.stringify(value) + '"';
14+
}
15+
16+
return key + '=' + value;
17+
};
18+
19+
export function logentriesFormatter(data: Record<string, unknown>) {
20+
return Object
21+
.keys(data)
22+
.map(key => convertToTag(data[key], key))
23+
.join(' ');
24+
}

src/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const logFactory = require('../index');
4-
const Logger = require('./logger/logger');
3+
const logFactory = require('./index');
4+
const { Logger } = require('./logger/logger');
55

66
describe('LogFactory', function() {
77
beforeEach(function() {

src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Logger, LoggerConfig } from './logger/logger';
2+
export { Logger, LoggerConfig } from './logger/logger';
3+
import { Timer } from './timer/timer';
4+
export { Timer } from './timer/timer';
5+
import { isNamespaceEnabled } from './enabled/enabled';
6+
import { formatter } from './formatter';
7+
8+
export function logFactory(namespace: string): Logger {
9+
return new Logger(namespace, isNamespaceEnabled(
10+
logFactory.getNamespaces(), namespace
11+
));
12+
}
13+
14+
logFactory.Logger = Logger;
15+
logFactory.Timer = Timer;
16+
logFactory.getNamespaces = function(): string {
17+
return process.env.DEBUG || '';
18+
};
19+
logFactory.configure = function(options: LoggerConfig): void {
20+
Logger.configure(options);
21+
};
22+
logFactory.formatter = formatter;
23+
24+
export default logFactory;
25+
module.exports = logFactory;

0 commit comments

Comments
 (0)