Skip to content

Commit bbd5499

Browse files
authored
Merge pull request #11 from fullstack-build/development
Add additional settings for output control
2 parents 498a97a + 7446c92 commit bbd5499

30 files changed

Lines changed: 1837 additions & 296 deletions

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
eqeqeq: [2, "smart"],
99
},
1010
extends: ["@rushstack/eslint-config", "plugin:prettier/recommended"],
11-
ignorePatterns: ["node_modules/", "dist/", "tests", "*.test.ts", "example/"],
11+
ignorePatterns: ["node_modules/", "dist/", "tests/", "*.test.ts", "example/"],
1212
env: {
1313
node: true,
1414
},

.githooks/pre-commit/prettier_eslint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.ts" | sed 's| |\\ |g
1010
echo "$FILES" | xargs ./node_modules/.bin/prettier --write
1111

1212
# ESLint
13-
echo "$FILES" | xargs ./node_modules/.bin/eslint --ext .ts --ignore-pattern tests/*
13+
echo "$FILES" | xargs ./node_modules/.bin/eslint --ext .ts --ignore-pattern 'tests/*' --ignore-pattern 'tests/**/*'
1414
eslint_exit=$?
1515

1616
# Add back the modified/prettified files to staging

CHANGELOG.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
# Change Log - tslog
22

3+
## 2.2.0:
4+
Fri, 05 Jun 2020
5+
Added additional output settings:
6+
* dateTimePattern: DateTime pattern based on Intl.DateTimeFormat.formatToParts with additional milliseconds, default: `year-month-day hour:minute:second.millisecond`
7+
* dateTimeTimezone: DateTime timezone, e.g. `utc`, or `Europe/Berlin`, `Europe/Moscow`. You can use `Intl.DateTimeFormat().resolvedOptions().timeZone` for local timezone, default: "utc"
8+
* printLogMessageInNewLine: Print log message in a new line below meta information, default: `false`
9+
* displayDateTime: Display date time at the beginning of a log message, default: `true`
10+
* displayLogLevel: Display log level, default: `true`
11+
* displayInstanceName: Display instanceName or not, default: `false`
12+
* displayLoggerName: Display name of the logger. Will only be visible if `name` was set, default: `true`
13+
* displayFilePath: Display file path ("hidden" | "displayAll" | "hideNodeModulesOnly"), default "hideNodeModulesOnly"
14+
* displayFunctionName: Display function name, default: `true`
15+
316
## 2.1.0:
4-
Sun, 26 Mai 2020 21:50:19 GMT
17+
Sun, 26 Mai 2020
518
- Exposed helper method `prettyError` that helps pretty-printing an error without logging with various options
619
- Adjust default error colors
720

821
## 2.0.0:
9-
Sun, 24 Mai 2020 23:35:19 GMT
22+
Sun, 24 Mai 2020
1023
- Setting `logAsJson` replaced with `type` = `pretty``json` ('pretty' is default)
1124
- `chalk` dependency removed (hexadecimal colors are no longer supported)
1225
- Color settings based on Node.js `utils.inspect.colors`
1326
- Error object displays additional `details` and exposes `nativeError`
1427
- When `type` is set to `json`, hide `nativeError` and expose stringified version as `errorString`
1528

1629
## 1.0.0
17-
Thu, 30 Apr 2020 07:01:19 GMT
30+
Thu, 30 Apr 2020
1831

1932
*Initial release*
2033

README.md

Lines changed: 157 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,7 @@ Structured (aka. _pretty_) log level output would look like this:
131131

132132
> **Hint:** Each logging method has a return type, which is a _JSON_ representation of the log message (`ILogObject`).
133133
> You can use this object to access its stack trace etc.
134-
```typescript
135-
import { Logger, ILogObject } from "tslog";
136-
137-
const log: Logger = new Logger();
138-
139-
const logWithTrace: ILogObject = log.trace("I am a trace log with a stack trace.");
140-
141-
console.log(JSON.stringify(logWithTrace, null, 2));
142-
```
134+
> <a href="#logObject">More details</a>
143135
144136
#### Settings
145137

@@ -188,7 +180,7 @@ const logger: Logger = new Logger({ displayInstanceName: true, instanceName: "AB
188180
##### `name`
189181
```default: undefined```
190182

191-
Each logger has an optional name, that is hidden by default.
183+
Each logger has an optional name, that is hidden by default. You can change this behavior by setting `displayLoggerName` to `true`.
192184
This setting is particularly interesting when working in a `monorepo`,
193185
giving you the possibility to provide each module/package with its own logger and being able to distinguish logs coming from different parts of your application.
194186

@@ -295,6 +287,64 @@ This setting allows you to overwrite the default colors of `tslog` used for the
295287

296288
More Details: <a href="https://nodejs.org/api/util.html#util_customizing_util_inspect_colors" target="_blank">Customizing util.inspect colors</a>
297289

290+
##### `dateTimePattern`
291+
```default: "year-month-day hour:minute:second.millisecond"```
292+
293+
Change the way `tslog` prints out the date.
294+
Based on Intl.DateTimeFormat.formatToParts with additional milliseconds, you can use type as a placeholder.
295+
Available placeholders are: `day`, `dayPeriod`, `era`, `hour`, `literal`, `minute`, `month`, `second`, `millisecond`, `timeZoneName`, `weekday` and `year`.
296+
297+
##### `dateTimeTimezone`
298+
```default: "utc" ```
299+
300+
Define in which timezone the date should be printed in.
301+
Possible values are `utc` and <a href="https://www.iana.org/time-zones" target="_blank">IANA (Internet Assigned Numbers Authority)</a> based timezones, e.g. `Europe/Berlin`, `Europe/Moscow` and so on.
302+
303+
> *Hint:* If you want to use your local time zone, you can set:
304+
> `dateTimeTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone`
305+
306+
##### `printLogMessageInNewLine`
307+
```default: false ```
308+
309+
By default `tslog` uses `tab` delimiters for separation of the meta information (date, log level, etc.) and the log parameters.
310+
Since the meta information can become quite long, you may want to prefer to print the log attributes in a new line.
311+
312+
##### `displayDateTime`
313+
```default: false ```
314+
315+
Defines whether the date time should be displayed.
316+
317+
##### `displayLogLevel`
318+
```default: true ```
319+
320+
Defines whether the log level should be displayed.
321+
322+
##### `displayInstanceName`
323+
```default: false ```
324+
325+
Defines whether the instance name (e.g. host name) should be displayed.
326+
327+
##### `displayLoggerName`
328+
```default: true ```
329+
330+
Defines whether the optional logger name should be displayed.
331+
332+
##### `displayFunctionName`
333+
```default: true ```
334+
335+
Defines whether the class and method or function name should be displayed.
336+
337+
338+
##### `displayFilePath`
339+
```default: hideNodeModulesOnly ```
340+
341+
Defines whether file path and line should be displayed or not.
342+
There are 3 possible settgins:
343+
* `hidden`
344+
* `displayAll`
345+
* `hideNodeModulesOnly` (default): This setting will hide all file paths containing `node_modules`.
346+
347+
298348
##### `stdOut` and `stdErr`
299349

300350
This both settings allow you to replace the default `stdOut` and `stdErr` _WriteStreams_.
@@ -314,7 +364,103 @@ All of them could be potentially handled by the same function, though.
314364

315365
Each _transport_ can have its own `minLevel`.
316366

317-
##### Simple transport example
367+
#### <a name="logObject"></a>Log object
368+
369+
<a href="https://tslog.js.org/tsdoc/interfaces/ilogobject.html" target="_blank">TSDoc: `interface: ILogObject`</a>
370+
371+
Internally `tslog` creates an object representing every available information around a particular log message, including errors, stack trace etc.
372+
This information can become quite handy in case you want to work with this data or forward it to an external log service.
373+
374+
```typescript
375+
interface ILogObject {
376+
/** Optional name of the instance this application is running on. */
377+
instanceName?: string;
378+
/** Optional name of the logger or empty string. */
379+
loggerName?: string;
380+
/* Name of the host */
381+
hostname: string;
382+
/** Timestamp */
383+
date: Date;
384+
/** Log level name (e.g. debug) */
385+
logLevel: silly | trace | debug | info | warn | error | fatal;
386+
/** Log level ID (e.g. 3) */
387+
logLevelId: 0 | 1 | 2 | 3 | 4 | 5 | 6;
388+
/** Log arguments */
389+
argumentsArray: (unknown | {
390+
/** Is this object an error? */
391+
isError: true;
392+
/** Name of the error*/
393+
name: string;
394+
/** Error message */
395+
message: string;
396+
/** additional Error details */
397+
details: object;
398+
/** native Error object */
399+
nativeError: Error;
400+
/** Stack trace of the error */
401+
stack: IStackFrame[];
402+
/** Code frame of the error */
403+
codeFrame?: {
404+
firstLineNumber: number;
405+
lineNumber: number;
406+
columnNumber: number | null;
407+
linesBefore: string[];
408+
relevantLine: string;
409+
linesAfter: string[];
410+
};
411+
})[];
412+
/** Optional Log stack trace */
413+
stack?: {
414+
/** Relative path based on the main folder */
415+
filePath: string;
416+
/** Full path */
417+
fullFilePath: string;
418+
/** Name of the file */
419+
fileName: string;
420+
/** Line number */
421+
lineNumber: number | null;
422+
/** Column Name */
423+
columnNumber: number | null;
424+
/** Called from constructor */
425+
isConstructor: boolean | null;
426+
/** Name of the function */
427+
functionName: string | null;
428+
/** Name of the class */
429+
typeName: string | null;
430+
/** Name of the Method */
431+
methodName: string | null;
432+
}[];
433+
}
434+
```
435+
436+
There are three ways to access this object:
437+
438+
##### Returned by each log method
439+
440+
```typescript
441+
import { Logger, ILogObject } from "tslog";
442+
443+
const log: Logger = new Logger();
444+
445+
const logWithTrace: ILogObject = log.trace("I am a trace log with a stack trace.");
446+
447+
console.log(JSON.stringify(logWithTrace, null, 2));
448+
```
449+
450+
##### Printed out in _JSON_ mode
451+
452+
```typescript
453+
new Logger({ type: "json" });
454+
```
455+
Resulting in the following output:
456+
![tslog log level json](https://raw.githubusercontent.com/fullstack-build/tslog/master/docs/assets/tslog_log_level_json.png)
457+
458+
459+
##### Forwarded to an attached transport
460+
461+
<a href="#transport">More details below</a>
462+
463+
##### <a name="transport"></a>Simple transport example
318464

319465
Here is a very simple implementation used in our _jest_ tests:
320466
```typescript

docs/api_extractor/tslog.api.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,21 @@ export interface ILogObject extends IStackFrame {
9797
// @public
9898
export interface ISettings extends ISettingsParam {
9999
// (undocumented)
100-
displayInstanceName?: boolean;
100+
dateTimePattern: string;
101+
// (undocumented)
102+
dateTimeTimezone: string;
103+
// (undocumented)
104+
displayDateTime: boolean;
105+
// (undocumented)
106+
displayFilePath?: "hidden" | "displayAll" | "hideNodeModulesOnly";
107+
// (undocumented)
108+
displayFunctionName?: boolean;
109+
// (undocumented)
110+
displayInstanceName: boolean;
111+
// (undocumented)
112+
displayLoggerName?: boolean;
113+
// (undocumented)
114+
displayLogLevel: boolean;
101115
// (undocumented)
102116
exposeErrorCodeFrame: boolean;
103117
// (undocumented)
@@ -121,6 +135,8 @@ export interface ISettings extends ISettingsParam {
121135
// (undocumented)
122136
prettyInspectOptions: InspectOptions;
123137
// (undocumented)
138+
printLogMessageInNewLine: boolean;
139+
// (undocumented)
124140
setCallerAsLoggerName: boolean;
125141
// (undocumented)
126142
stdErr: IStd;
@@ -134,7 +150,14 @@ export interface ISettings extends ISettingsParam {
134150

135151
// @public
136152
export interface ISettingsParam {
153+
dateTimePattern?: string;
154+
dateTimeTimezone?: string;
155+
displayDateTime?: boolean;
156+
displayFilePath?: "hidden" | "displayAll" | "hideNodeModulesOnly";
157+
displayFunctionName?: boolean;
137158
displayInstanceName?: boolean;
159+
displayLoggerName?: boolean;
160+
displayLogLevel?: boolean;
138161
exposeErrorCodeFrame?: boolean;
139162
exposeErrorCodeFrameLinesBeforeAndAfter?: number;
140163
exposeStack?: boolean;
@@ -146,6 +169,7 @@ export interface ISettingsParam {
146169
overwriteConsole?: boolean;
147170
prettyInspectHighlightStyles?: IHighlightStyles;
148171
prettyInspectOptions?: InspectOptions;
172+
printLogMessageInNewLine?: boolean;
149173
setCallerAsLoggerName?: boolean;
150174
stdErr?: IStd;
151175
stdOut?: IStd;

docs/tsdoc/assets/js/search.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)