namirasoft-log 1.0.3 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitlab-ci.yml +14 -0
- package/README.md +204 -204
- package/package.json +11 -11
- package/src/BaseFormatter.ts +27 -27
- package/src/FormatterFull.ts +21 -21
- package/src/FormatterShort.ts +14 -14
- package/src/IFormatter.ts +5 -5
- package/src/ILogger.ts +16 -16
- package/src/IStream.ts +4 -4
- package/src/Log.ts +17 -17
- package/src/LogLevel.ts +10 -10
- package/src/Logger.ts +100 -100
- package/src/NamirasoftLogServer.ts +2 -2
- package/src/StreamConsole.ts +30 -30
- package/src/StreamFile.ts +51 -51
- package/src/StreamNamirasoftLog.ts +13 -13
- package/src/index.ts +12 -12
- package/tsconfig.json +29 -29
package/.gitlab-ci.yml
ADDED
package/README.md
CHANGED
|
@@ -1,205 +1,205 @@
|
|
|
1
|
-
|
|
2
|
-
# Namirasoft TS SDK Log
|
|
3
|
-
|
|
4
|
-
**Namira Software Corporation Log Package**
|
|
5
|
-
is a powerful Node.js library designed to simplify the management of logging, error handling, and notifications within your applications. With a focus on flexibility and extensibility, this library empowers developers to effortlessly control how their applications log information, handle errors, and send notifications across various channels such as Telegram, email, SMS, and push notifications. Whether you need to track critical events, respond to unhandled exceptions, or deliver real-time alerts to different stakeholders, it provides a robust solution with a straightforward API. Customize the behavior to suit your unique use case and seamlessly integrate it into your projects to enhance both monitoring and communication capabilities. Streamline your development workflow and enhance the reliability of your applications with Namira Software Corporation Log Package.
|
|
6
|
-
[Namira Software Corporation](https://namirasoft.com)
|
|
7
|
-
|
|
8
|
-
## Features
|
|
9
|
-
|
|
10
|
-
This library provides the following key features:
|
|
11
|
-
|
|
12
|
-
1. **Logging:** Log messages to various sources such as the console, files, database and [Namirasoft Log Server](https://log.namirasoft.com)
|
|
13
|
-
2. **Error Handling:** Catch all unhandled errors in your Node.js application.
|
|
14
|
-
3. **Notifications:** Send messages and alerts through different media (Email, SMS, Telegram, push notifications, and etc.) based on configurable conditions. You can find more here [Namirasoft Log Server](https://log.namirasoft.com)
|
|
15
|
-
|
|
16
|
-
You can customize the library's behavior and integrate it with various notification platforms to suit your specific use case.
|
|
17
|
-
|
|
18
|
-
## Installation
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install namirasoft-log
|
|
22
|
-
```
|
|
23
|
-
## Import
|
|
24
|
-
First you need to import it.
|
|
25
|
-
- JavaScript
|
|
26
|
-
```js
|
|
27
|
-
let Logger = require("namirasoft-log");
|
|
28
|
-
```
|
|
29
|
-
- TypeScript
|
|
30
|
-
```js
|
|
31
|
-
import { Logger } from "namirasoft-log";
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
## Make a Logger
|
|
35
|
-
To make a logger please use this:
|
|
36
|
-
|
|
37
|
-
- JavaScript
|
|
38
|
-
```js
|
|
39
|
-
let logger = new Logger();
|
|
40
|
-
```
|
|
41
|
-
- TypeScript
|
|
42
|
-
```js
|
|
43
|
-
let logger: Logger = new Logger();
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Usage
|
|
47
|
-
#### Normal Logging
|
|
48
|
-
|
|
49
|
-
```js
|
|
50
|
-
logger.trace("This is a test for trace");
|
|
51
|
-
logger.verbose("This is a test for verbose");
|
|
52
|
-
logger.debug("This is a test for debug");
|
|
53
|
-
logger.info("This is a test for info");
|
|
54
|
-
logger.warning("This is a test for warning");
|
|
55
|
-
logger.error("This is a test for error");
|
|
56
|
-
logger.critical("This is a test for critical");
|
|
57
|
-
logger.fatal("This is a test for fatal");
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
#### Grouping Logs
|
|
61
|
-
```js
|
|
62
|
-
logger.error("Out of stuck", "Payment");
|
|
63
|
-
logger.error("Can not find your account", "Account");
|
|
64
|
-
logger.warning("You account is not verified yet.", "Account");
|
|
65
|
-
logger.Info("Invoice has been paid successfully", "Payment");
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
#### Extra and Stack information
|
|
69
|
-
```js
|
|
70
|
-
logger.debug("message", "group", "More details go here");
|
|
71
|
-
logger.error("message", "group", "More details go here");
|
|
72
|
-
logger.warning("message", "group", "More details go here");
|
|
73
|
-
logger.fatal("message", "group", "More details go here");
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
#### Loggin errors on catch
|
|
77
|
-
```js
|
|
78
|
-
try
|
|
79
|
-
{
|
|
80
|
-
// some bad codes here making error
|
|
81
|
-
}
|
|
82
|
-
catch(e)
|
|
83
|
-
{
|
|
84
|
-
logger.onCatchError(e);
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
```js
|
|
88
|
-
try
|
|
89
|
-
{
|
|
90
|
-
// some bad codes here making error
|
|
91
|
-
}
|
|
92
|
-
catch(e)
|
|
93
|
-
{
|
|
94
|
-
logger.onCatchCritical(e);
|
|
95
|
-
}
|
|
96
|
-
```
|
|
97
|
-
```js
|
|
98
|
-
try
|
|
99
|
-
{
|
|
100
|
-
// some bad codes here making error
|
|
101
|
-
}
|
|
102
|
-
catch(e)
|
|
103
|
-
{
|
|
104
|
-
logger.onCatchFatal(e);
|
|
105
|
-
}
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## Error Handling
|
|
109
|
-
To handle all unhandled exceptions on the entire project:
|
|
110
|
-
```js
|
|
111
|
-
logger.initUncought();
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
## Streams
|
|
115
|
-
### Console
|
|
116
|
-
To add a stream on console which is the default stream use:
|
|
117
|
-
```js
|
|
118
|
-
import { StreamConsole } from "namirasoft-node";
|
|
119
|
-
|
|
120
|
-
logger.addStream(new StreamConsole());
|
|
121
|
-
```
|
|
122
|
-
### File
|
|
123
|
-
|
|
124
|
-
To add a stream on file:
|
|
125
|
-
```js
|
|
126
|
-
import { StreamFile} from "namirasoft-node";
|
|
127
|
-
let streamFile = new StreamFile("Your-Custom-Path"); // your can pss "" for argument
|
|
128
|
-
streamFile.seprateByDate = true; // this is the default value
|
|
129
|
-
streamFile.seprateByGroup = false; // this is the default value
|
|
130
|
-
streamFile.seprateByLevel = true; // this is the default value
|
|
131
|
-
logger.addStream(streamFile);
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### Namirasoft Log
|
|
135
|
-
To add a stream on Namirasoft Log API which gives which is the default stream use:
|
|
136
|
-
```js
|
|
137
|
-
import { StreamNamirasoftLog } from "namirasoft-node";
|
|
138
|
-
logger.addStream(new StreamNamirasoftLog("Your-Token"));
|
|
139
|
-
```
|
|
140
|
-
You can get your token [Here](https://log.namirasoft.com) after creating your account and project.
|
|
141
|
-
|
|
142
|
-
### Custom
|
|
143
|
-
It is also possible to create custom stream like for database:
|
|
144
|
-
|
|
145
|
-
fist create your custom class like
|
|
146
|
-
```ts
|
|
147
|
-
import { IStream } from "./IStream";
|
|
148
|
-
import { Log } from "./Log";
|
|
149
|
-
import { LogLevel } from './LogLevel';
|
|
150
|
-
|
|
151
|
-
export class MyCustomStream implements IStream
|
|
152
|
-
{
|
|
153
|
-
async write(log: Log, formated: string[]): Promise<void>
|
|
154
|
-
{
|
|
155
|
-
// save log or formated
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
```
|
|
159
|
-
Then add it to your logger
|
|
160
|
-
```ts
|
|
161
|
-
logger.addStream(new MyCustomStream());
|
|
162
|
-
```
|
|
163
|
-
You can get your token [Here](https://log.namirasoft.com) after creating your account and project.
|
|
164
|
-
|
|
165
|
-
## Formatters
|
|
166
|
-
|
|
167
|
-
### Full
|
|
168
|
-
To set a full formatter which is the default formatter use:
|
|
169
|
-
```js
|
|
170
|
-
import { FormatterFull } from "namirasoft-node";
|
|
171
|
-
logger.setFromatter(new FormatterFull());
|
|
172
|
-
```
|
|
173
|
-
### Short
|
|
174
|
-
To set a short formatter use:
|
|
175
|
-
```js
|
|
176
|
-
import { FormatterShort } from "namirasoft-node";
|
|
177
|
-
logger.setFromatter(new FormatterShort());
|
|
178
|
-
```
|
|
179
|
-
### Level-Based
|
|
180
|
-
To set a different formatter for each Log Level use:
|
|
181
|
-
```js
|
|
182
|
-
import { LogLevel, FormatterShort, FormatterFull } from "namirasoft-node";
|
|
183
|
-
|
|
184
|
-
logger.setCustomFormatter(LogLevel.Info, new FormatterShort());
|
|
185
|
-
logger.setCustomFormatter(LogLevel.Error, new FormatterFull());
|
|
186
|
-
```
|
|
187
|
-
### Custom
|
|
188
|
-
To make a custom formatter make a formatter class like:
|
|
189
|
-
```ts
|
|
190
|
-
import { BaseFormatter, IFormatter, Log } from "namirasoft-node";
|
|
191
|
-
|
|
192
|
-
export class MyCustomFormatter extends BaseFormatter implements IFormatter
|
|
193
|
-
{
|
|
194
|
-
async format(log: Log): Promise<string[]>
|
|
195
|
-
{
|
|
196
|
-
let ans: string[] = [];
|
|
197
|
-
ans.push(this.formatDateTime(log.date) + ": " + log.message);
|
|
198
|
-
return ans;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
```
|
|
202
|
-
Then add it to your logger
|
|
203
|
-
```ts
|
|
204
|
-
logger.addStream(new MyCustomFormatter());
|
|
1
|
+
|
|
2
|
+
# Namirasoft TS SDK Log
|
|
3
|
+
|
|
4
|
+
**Namira Software Corporation Log Package**
|
|
5
|
+
is a powerful Node.js library designed to simplify the management of logging, error handling, and notifications within your applications. With a focus on flexibility and extensibility, this library empowers developers to effortlessly control how their applications log information, handle errors, and send notifications across various channels such as Telegram, email, SMS, and push notifications. Whether you need to track critical events, respond to unhandled exceptions, or deliver real-time alerts to different stakeholders, it provides a robust solution with a straightforward API. Customize the behavior to suit your unique use case and seamlessly integrate it into your projects to enhance both monitoring and communication capabilities. Streamline your development workflow and enhance the reliability of your applications with Namira Software Corporation Log Package.
|
|
6
|
+
[Namira Software Corporation](https://namirasoft.com)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
This library provides the following key features:
|
|
11
|
+
|
|
12
|
+
1. **Logging:** Log messages to various sources such as the console, files, database and [Namirasoft Log Server](https://log.namirasoft.com)
|
|
13
|
+
2. **Error Handling:** Catch all unhandled errors in your Node.js application.
|
|
14
|
+
3. **Notifications:** Send messages and alerts through different media (Email, SMS, Telegram, push notifications, and etc.) based on configurable conditions. You can find more here [Namirasoft Log Server](https://log.namirasoft.com)
|
|
15
|
+
|
|
16
|
+
You can customize the library's behavior and integrate it with various notification platforms to suit your specific use case.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install namirasoft-log
|
|
22
|
+
```
|
|
23
|
+
## Import
|
|
24
|
+
First you need to import it.
|
|
25
|
+
- JavaScript
|
|
26
|
+
```js
|
|
27
|
+
let Logger = require("namirasoft-log");
|
|
28
|
+
```
|
|
29
|
+
- TypeScript
|
|
30
|
+
```js
|
|
31
|
+
import { Logger } from "namirasoft-log";
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Make a Logger
|
|
35
|
+
To make a logger please use this:
|
|
36
|
+
|
|
37
|
+
- JavaScript
|
|
38
|
+
```js
|
|
39
|
+
let logger = new Logger();
|
|
40
|
+
```
|
|
41
|
+
- TypeScript
|
|
42
|
+
```js
|
|
43
|
+
let logger: Logger = new Logger();
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
#### Normal Logging
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
logger.trace("This is a test for trace");
|
|
51
|
+
logger.verbose("This is a test for verbose");
|
|
52
|
+
logger.debug("This is a test for debug");
|
|
53
|
+
logger.info("This is a test for info");
|
|
54
|
+
logger.warning("This is a test for warning");
|
|
55
|
+
logger.error("This is a test for error");
|
|
56
|
+
logger.critical("This is a test for critical");
|
|
57
|
+
logger.fatal("This is a test for fatal");
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### Grouping Logs
|
|
61
|
+
```js
|
|
62
|
+
logger.error("Out of stuck", "Payment");
|
|
63
|
+
logger.error("Can not find your account", "Account");
|
|
64
|
+
logger.warning("You account is not verified yet.", "Account");
|
|
65
|
+
logger.Info("Invoice has been paid successfully", "Payment");
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### Extra and Stack information
|
|
69
|
+
```js
|
|
70
|
+
logger.debug("message", "group", "More details go here");
|
|
71
|
+
logger.error("message", "group", "More details go here");
|
|
72
|
+
logger.warning("message", "group", "More details go here");
|
|
73
|
+
logger.fatal("message", "group", "More details go here");
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### Loggin errors on catch
|
|
77
|
+
```js
|
|
78
|
+
try
|
|
79
|
+
{
|
|
80
|
+
// some bad codes here making error
|
|
81
|
+
}
|
|
82
|
+
catch(e)
|
|
83
|
+
{
|
|
84
|
+
logger.onCatchError(e);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
```js
|
|
88
|
+
try
|
|
89
|
+
{
|
|
90
|
+
// some bad codes here making error
|
|
91
|
+
}
|
|
92
|
+
catch(e)
|
|
93
|
+
{
|
|
94
|
+
logger.onCatchCritical(e);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
```js
|
|
98
|
+
try
|
|
99
|
+
{
|
|
100
|
+
// some bad codes here making error
|
|
101
|
+
}
|
|
102
|
+
catch(e)
|
|
103
|
+
{
|
|
104
|
+
logger.onCatchFatal(e);
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Error Handling
|
|
109
|
+
To handle all unhandled exceptions on the entire project:
|
|
110
|
+
```js
|
|
111
|
+
logger.initUncought();
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Streams
|
|
115
|
+
### Console
|
|
116
|
+
To add a stream on console which is the default stream use:
|
|
117
|
+
```js
|
|
118
|
+
import { StreamConsole } from "namirasoft-node";
|
|
119
|
+
|
|
120
|
+
logger.addStream(new StreamConsole());
|
|
121
|
+
```
|
|
122
|
+
### File
|
|
123
|
+
|
|
124
|
+
To add a stream on file:
|
|
125
|
+
```js
|
|
126
|
+
import { StreamFile} from "namirasoft-node";
|
|
127
|
+
let streamFile = new StreamFile("Your-Custom-Path"); // your can pss "" for argument
|
|
128
|
+
streamFile.seprateByDate = true; // this is the default value
|
|
129
|
+
streamFile.seprateByGroup = false; // this is the default value
|
|
130
|
+
streamFile.seprateByLevel = true; // this is the default value
|
|
131
|
+
logger.addStream(streamFile);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Namirasoft Log
|
|
135
|
+
To add a stream on Namirasoft Log API which gives which is the default stream use:
|
|
136
|
+
```js
|
|
137
|
+
import { StreamNamirasoftLog } from "namirasoft-node";
|
|
138
|
+
logger.addStream(new StreamNamirasoftLog("Your-Token"));
|
|
139
|
+
```
|
|
140
|
+
You can get your token [Here](https://log.namirasoft.com) after creating your account and project.
|
|
141
|
+
|
|
142
|
+
### Custom
|
|
143
|
+
It is also possible to create custom stream like for database:
|
|
144
|
+
|
|
145
|
+
fist create your custom class like
|
|
146
|
+
```ts
|
|
147
|
+
import { IStream } from "./IStream";
|
|
148
|
+
import { Log } from "./Log";
|
|
149
|
+
import { LogLevel } from './LogLevel';
|
|
150
|
+
|
|
151
|
+
export class MyCustomStream implements IStream
|
|
152
|
+
{
|
|
153
|
+
async write(log: Log, formated: string[]): Promise<void>
|
|
154
|
+
{
|
|
155
|
+
// save log or formated
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
Then add it to your logger
|
|
160
|
+
```ts
|
|
161
|
+
logger.addStream(new MyCustomStream());
|
|
162
|
+
```
|
|
163
|
+
You can get your token [Here](https://log.namirasoft.com) after creating your account and project.
|
|
164
|
+
|
|
165
|
+
## Formatters
|
|
166
|
+
|
|
167
|
+
### Full
|
|
168
|
+
To set a full formatter which is the default formatter use:
|
|
169
|
+
```js
|
|
170
|
+
import { FormatterFull } from "namirasoft-node";
|
|
171
|
+
logger.setFromatter(new FormatterFull());
|
|
172
|
+
```
|
|
173
|
+
### Short
|
|
174
|
+
To set a short formatter use:
|
|
175
|
+
```js
|
|
176
|
+
import { FormatterShort } from "namirasoft-node";
|
|
177
|
+
logger.setFromatter(new FormatterShort());
|
|
178
|
+
```
|
|
179
|
+
### Level-Based
|
|
180
|
+
To set a different formatter for each Log Level use:
|
|
181
|
+
```js
|
|
182
|
+
import { LogLevel, FormatterShort, FormatterFull } from "namirasoft-node";
|
|
183
|
+
|
|
184
|
+
logger.setCustomFormatter(LogLevel.Info, new FormatterShort());
|
|
185
|
+
logger.setCustomFormatter(LogLevel.Error, new FormatterFull());
|
|
186
|
+
```
|
|
187
|
+
### Custom
|
|
188
|
+
To make a custom formatter make a formatter class like:
|
|
189
|
+
```ts
|
|
190
|
+
import { BaseFormatter, IFormatter, Log } from "namirasoft-node";
|
|
191
|
+
|
|
192
|
+
export class MyCustomFormatter extends BaseFormatter implements IFormatter
|
|
193
|
+
{
|
|
194
|
+
async format(log: Log): Promise<string[]>
|
|
195
|
+
{
|
|
196
|
+
let ans: string[] = [];
|
|
197
|
+
ans.push(this.formatDateTime(log.date) + ": " + log.message);
|
|
198
|
+
return ans;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
Then add it to your logger
|
|
203
|
+
```ts
|
|
204
|
+
logger.addStream(new MyCustomFormatter());
|
|
205
205
|
```
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
"name": "namirasoft-log",
|
|
3
|
+
"description": "Namira Software Corporation Log NPM Package",
|
|
4
|
+
"version": "1.1.1",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"scripts": {},
|
|
8
|
+
"author": "Amir Abolhasani",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@types/node": "^20.9.2"
|
|
12
|
+
}
|
|
13
13
|
}
|
package/src/BaseFormatter.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
export abstract class BaseFormatter
|
|
2
|
-
{
|
|
3
|
-
protected formatDateTime(date: Date)
|
|
4
|
-
{
|
|
5
|
-
return this.formatDate(date) + " " + this.formatTime(date);
|
|
6
|
-
}
|
|
7
|
-
protected formatDate(date: Date)
|
|
8
|
-
{
|
|
9
|
-
const year = date.getFullYear();
|
|
10
|
-
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
11
|
-
const day = String(date.getDate()).padStart(2, '0');
|
|
12
|
-
return `${year}-${month}-${day}`;
|
|
13
|
-
}
|
|
14
|
-
protected formatTime(date: Date)
|
|
15
|
-
{
|
|
16
|
-
const hours = String(date.getHours()).padStart(2, '0');
|
|
17
|
-
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
18
|
-
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
19
|
-
return `${hours}:${minutes}:${seconds}`;
|
|
20
|
-
}
|
|
21
|
-
protected qoute(text: string, size: number, filler: string)
|
|
22
|
-
{
|
|
23
|
-
let count = (size - text.length) / 2;
|
|
24
|
-
text = text.padStart(text.length + count, filler);
|
|
25
|
-
text = text.padEnd(size, filler);
|
|
26
|
-
return text;
|
|
27
|
-
}
|
|
1
|
+
export abstract class BaseFormatter
|
|
2
|
+
{
|
|
3
|
+
protected formatDateTime(date: Date)
|
|
4
|
+
{
|
|
5
|
+
return this.formatDate(date) + " " + this.formatTime(date);
|
|
6
|
+
}
|
|
7
|
+
protected formatDate(date: Date)
|
|
8
|
+
{
|
|
9
|
+
const year = date.getFullYear();
|
|
10
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
11
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
12
|
+
return `${year}-${month}-${day}`;
|
|
13
|
+
}
|
|
14
|
+
protected formatTime(date: Date)
|
|
15
|
+
{
|
|
16
|
+
const hours = String(date.getHours()).padStart(2, '0');
|
|
17
|
+
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
18
|
+
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
19
|
+
return `${hours}:${minutes}:${seconds}`;
|
|
20
|
+
}
|
|
21
|
+
protected qoute(text: string, size: number, filler: string)
|
|
22
|
+
{
|
|
23
|
+
let count = (size - text.length) / 2;
|
|
24
|
+
text = text.padStart(text.length + count, filler);
|
|
25
|
+
text = text.padEnd(size, filler);
|
|
26
|
+
return text;
|
|
27
|
+
}
|
|
28
28
|
}
|
package/src/FormatterFull.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { BaseFormatter } from "./BaseFormatter";
|
|
2
|
-
import { IFormatter } from "./IFormatter";
|
|
3
|
-
import { Log } from "./Log";
|
|
4
|
-
import { LogLevel } from "./LogLevel";
|
|
5
|
-
|
|
6
|
-
export class FormatterFull extends BaseFormatter implements IFormatter
|
|
7
|
-
{
|
|
8
|
-
async format(log: Log): Promise<string[]>
|
|
9
|
-
{
|
|
10
|
-
let ans: string[] = [];
|
|
11
|
-
ans.push(this.qoute(this.formatDateTime(log.date), 100, '-'));
|
|
12
|
-
ans.push("Level: " + LogLevel[log.level]);
|
|
13
|
-
if (log.group)
|
|
14
|
-
ans.push("Group: " + log.group);
|
|
15
|
-
ans.push("Message: " + log.message);
|
|
16
|
-
if (log.stack)
|
|
17
|
-
ans.push("Stack: " + log.stack);
|
|
18
|
-
ans.push(this.qoute("", 100, '-'));
|
|
19
|
-
ans.push("");
|
|
20
|
-
return ans;
|
|
21
|
-
}
|
|
1
|
+
import { BaseFormatter } from "./BaseFormatter";
|
|
2
|
+
import { IFormatter } from "./IFormatter";
|
|
3
|
+
import { Log } from "./Log";
|
|
4
|
+
import { LogLevel } from "./LogLevel";
|
|
5
|
+
|
|
6
|
+
export class FormatterFull extends BaseFormatter implements IFormatter
|
|
7
|
+
{
|
|
8
|
+
async format(log: Log): Promise<string[]>
|
|
9
|
+
{
|
|
10
|
+
let ans: string[] = [];
|
|
11
|
+
ans.push(this.qoute(this.formatDateTime(log.date), 100, '-'));
|
|
12
|
+
ans.push("Level: " + LogLevel[log.level]);
|
|
13
|
+
if (log.group)
|
|
14
|
+
ans.push("Group: " + log.group);
|
|
15
|
+
ans.push("Message: " + log.message);
|
|
16
|
+
if (log.stack)
|
|
17
|
+
ans.push("Stack: " + log.stack);
|
|
18
|
+
ans.push(this.qoute("", 100, '-'));
|
|
19
|
+
ans.push("");
|
|
20
|
+
return ans;
|
|
21
|
+
}
|
|
22
22
|
}
|
package/src/FormatterShort.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { BaseFormatter } from "./BaseFormatter";
|
|
2
|
-
import { IFormatter } from "./IFormatter";
|
|
3
|
-
import { Log } from "./Log";
|
|
4
|
-
|
|
5
|
-
export class FormatterShort extends BaseFormatter implements IFormatter
|
|
6
|
-
{
|
|
7
|
-
async format(log: Log): Promise<string[]>
|
|
8
|
-
{
|
|
9
|
-
let ans: string[] = [];
|
|
10
|
-
ans.push(this.formatDateTime(log.date) + ": " + [log.group, log.message].filter(m => m).join(" - "));
|
|
11
|
-
if (log.stack)
|
|
12
|
-
ans.push(log.stack);
|
|
13
|
-
return ans;
|
|
14
|
-
}
|
|
1
|
+
import { BaseFormatter } from "./BaseFormatter";
|
|
2
|
+
import { IFormatter } from "./IFormatter";
|
|
3
|
+
import { Log } from "./Log";
|
|
4
|
+
|
|
5
|
+
export class FormatterShort extends BaseFormatter implements IFormatter
|
|
6
|
+
{
|
|
7
|
+
async format(log: Log): Promise<string[]>
|
|
8
|
+
{
|
|
9
|
+
let ans: string[] = [];
|
|
10
|
+
ans.push(this.formatDateTime(log.date) + ": " + [log.group, log.message].filter(m => m).join(" - "));
|
|
11
|
+
if (log.stack)
|
|
12
|
+
ans.push(log.stack);
|
|
13
|
+
return ans;
|
|
14
|
+
}
|
|
15
15
|
}
|
package/src/IFormatter.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Log } from "./Log";
|
|
2
|
-
|
|
3
|
-
export interface IFormatter
|
|
4
|
-
{
|
|
5
|
-
format(log: Log): Promise<string[]>;
|
|
1
|
+
import { Log } from "./Log";
|
|
2
|
+
|
|
3
|
+
export interface IFormatter
|
|
4
|
+
{
|
|
5
|
+
format(log: Log): Promise<string[]>;
|
|
6
6
|
}
|
package/src/ILogger.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { LogLevel } from "./LogLevel";
|
|
2
|
-
|
|
3
|
-
export interface ILogger
|
|
4
|
-
{
|
|
5
|
-
log(level: LogLevel, message: string, group?: string, stack?: string): Promise<void>;
|
|
6
|
-
trace(message: string, group?: string, stack?: string): Promise<void>;
|
|
7
|
-
verbose(message: string, group?: string, stack?: string): Promise<void>;
|
|
8
|
-
debug(message: string, group?: string, stack?: string): Promise<void>;
|
|
9
|
-
info(message: string, group?: string, stack?: string): Promise<void>;
|
|
10
|
-
warning(message: string, group?: string, stack?: string): Promise<void>;
|
|
11
|
-
error(message: string, group?: string, stack?: string): Promise<void>;
|
|
12
|
-
critical(message: string, group?: string, stack?: string): Promise<void>;
|
|
13
|
-
fatal(message: string, group?: string, stack?: string): Promise<void>;
|
|
14
|
-
onCatchError(error: Error | unknown, group?: string): Promise<void>;
|
|
15
|
-
onCatchCritical(error: Error | unknown, group?: string): Promise<void>;
|
|
16
|
-
onCatchFatal(error: Error | unknown, group?: string): Promise<void>;
|
|
1
|
+
import { LogLevel } from "./LogLevel";
|
|
2
|
+
|
|
3
|
+
export interface ILogger
|
|
4
|
+
{
|
|
5
|
+
log(level: LogLevel, message: string, group?: string, stack?: string): Promise<void>;
|
|
6
|
+
trace(message: string, group?: string, stack?: string): Promise<void>;
|
|
7
|
+
verbose(message: string, group?: string, stack?: string): Promise<void>;
|
|
8
|
+
debug(message: string, group?: string, stack?: string): Promise<void>;
|
|
9
|
+
info(message: string, group?: string, stack?: string): Promise<void>;
|
|
10
|
+
warning(message: string, group?: string, stack?: string): Promise<void>;
|
|
11
|
+
error(message: string, group?: string, stack?: string): Promise<void>;
|
|
12
|
+
critical(message: string, group?: string, stack?: string): Promise<void>;
|
|
13
|
+
fatal(message: string, group?: string, stack?: string): Promise<void>;
|
|
14
|
+
onCatchError(error: Error | unknown, group?: string): Promise<void>;
|
|
15
|
+
onCatchCritical(error: Error | unknown, group?: string): Promise<void>;
|
|
16
|
+
onCatchFatal(error: Error | unknown, group?: string): Promise<void>;
|
|
17
17
|
}
|
package/src/IStream.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Log } from "./Log";
|
|
2
|
-
export interface IStream
|
|
3
|
-
{
|
|
4
|
-
write(log: Log, formated: string[]): Promise<void>;
|
|
1
|
+
import { Log } from "./Log";
|
|
2
|
+
export interface IStream
|
|
3
|
+
{
|
|
4
|
+
write(log: Log, formated: string[]): Promise<void>;
|
|
5
5
|
}
|
package/src/Log.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { LogLevel } from "./LogLevel";
|
|
2
|
-
|
|
3
|
-
export class Log
|
|
4
|
-
{
|
|
5
|
-
date: Date;
|
|
6
|
-
level: LogLevel;
|
|
7
|
-
group: string;
|
|
8
|
-
message: string;
|
|
9
|
-
stack: string;
|
|
10
|
-
constructor(level: LogLevel, message: string, group?: string, stack?: string)
|
|
11
|
-
{
|
|
12
|
-
this.date = new Date();
|
|
13
|
-
this.level = level;
|
|
14
|
-
this.message = message;
|
|
15
|
-
this.group = group ?? "";
|
|
16
|
-
this.stack = stack ?? "";
|
|
17
|
-
}
|
|
1
|
+
import { LogLevel } from "./LogLevel";
|
|
2
|
+
|
|
3
|
+
export class Log
|
|
4
|
+
{
|
|
5
|
+
date: Date;
|
|
6
|
+
level: LogLevel;
|
|
7
|
+
group: string;
|
|
8
|
+
message: string;
|
|
9
|
+
stack: string;
|
|
10
|
+
constructor(level: LogLevel, message: string, group?: string, stack?: string)
|
|
11
|
+
{
|
|
12
|
+
this.date = new Date();
|
|
13
|
+
this.level = level;
|
|
14
|
+
this.message = message;
|
|
15
|
+
this.group = group ?? "";
|
|
16
|
+
this.stack = stack ?? "";
|
|
17
|
+
}
|
|
18
18
|
}
|
package/src/LogLevel.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export enum LogLevel
|
|
2
|
-
{
|
|
3
|
-
"Trace",
|
|
4
|
-
"Verbose",
|
|
5
|
-
"Debug",
|
|
6
|
-
"Info",
|
|
7
|
-
"Warning",
|
|
8
|
-
"Error",
|
|
9
|
-
"Critical",
|
|
10
|
-
"Fatal"
|
|
1
|
+
export enum LogLevel
|
|
2
|
+
{
|
|
3
|
+
"Trace",
|
|
4
|
+
"Verbose",
|
|
5
|
+
"Debug",
|
|
6
|
+
"Info",
|
|
7
|
+
"Warning",
|
|
8
|
+
"Error",
|
|
9
|
+
"Critical",
|
|
10
|
+
"Fatal"
|
|
11
11
|
}
|
package/src/Logger.ts
CHANGED
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import { FormatterFull } from "./FormatterFull";
|
|
2
|
-
import { IFormatter } from "./IFormatter";
|
|
3
|
-
import { ILogger } from "./ILogger";
|
|
4
|
-
import { IStream } from "./IStream";
|
|
5
|
-
import { Log } from "./Log";
|
|
6
|
-
import { LogLevel } from "./LogLevel";
|
|
7
|
-
import { StreamConsole } from "./StreamConsole";
|
|
8
|
-
|
|
9
|
-
export class Logger implements ILogger
|
|
10
|
-
{
|
|
11
|
-
private defaultStream: IStream = new StreamConsole();
|
|
12
|
-
private formatter?: IFormatter = new FormatterFull();
|
|
13
|
-
private customFormatter: { [level: string]: IFormatter } = {};
|
|
14
|
-
private streams: IStream[] = [];
|
|
15
|
-
setFromatter(formatter: IFormatter): void
|
|
16
|
-
{
|
|
17
|
-
this.formatter = formatter;
|
|
18
|
-
}
|
|
19
|
-
setCustomFormatter(level: LogLevel, formatter: IFormatter): void
|
|
20
|
-
{
|
|
21
|
-
this.customFormatter[level] = formatter;
|
|
22
|
-
}
|
|
23
|
-
addStream(stream: IStream)
|
|
24
|
-
{
|
|
25
|
-
this.streams.push(stream);
|
|
26
|
-
}
|
|
27
|
-
async log(level: LogLevel, message: string, group?: string, stack?: string)
|
|
28
|
-
{
|
|
29
|
-
let log = new Log(level, message, group, stack);
|
|
30
|
-
let formatted: string[] = [];
|
|
31
|
-
let formatter = this.customFormatter[level.toString()] ?? this.formatter;
|
|
32
|
-
if (formatter)
|
|
33
|
-
formatted = await formatter.format(log);
|
|
34
|
-
if (this.streams.length == 0)
|
|
35
|
-
this.defaultStream.write(log, formatted);
|
|
36
|
-
else
|
|
37
|
-
{
|
|
38
|
-
let ps = this.streams.map(s => s.write(log, formatted));
|
|
39
|
-
await Promise.all(ps);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
async trace(message: string, group?: string, stack?: string)
|
|
43
|
-
{
|
|
44
|
-
await this.log(LogLevel.Trace, message, group, stack);
|
|
45
|
-
}
|
|
46
|
-
async verbose(message: string, group?: string, stack?: string)
|
|
47
|
-
{
|
|
48
|
-
await this.log(LogLevel.Verbose, message, group, stack);
|
|
49
|
-
}
|
|
50
|
-
async debug(message: string, group?: string, stack?: string)
|
|
51
|
-
{
|
|
52
|
-
await this.log(LogLevel.Debug, message, group, stack);
|
|
53
|
-
}
|
|
54
|
-
async info(message: string, group?: string, stack?: string)
|
|
55
|
-
{
|
|
56
|
-
await this.log(LogLevel.Info, message, group, stack);
|
|
57
|
-
}
|
|
58
|
-
async warning(message: string, group?: string, stack?: string)
|
|
59
|
-
{
|
|
60
|
-
await this.log(LogLevel.Warning, message, group, stack);
|
|
61
|
-
}
|
|
62
|
-
async error(message: string, group?: string, stack?: string)
|
|
63
|
-
{
|
|
64
|
-
await this.log(LogLevel.Error, message, group, stack);
|
|
65
|
-
}
|
|
66
|
-
async critical(message: string, group?: string, stack?: string)
|
|
67
|
-
{
|
|
68
|
-
await this.log(LogLevel.Critical, message, group, stack);
|
|
69
|
-
}
|
|
70
|
-
async fatal(message: string, group?: string, stack?: string)
|
|
71
|
-
{
|
|
72
|
-
await this.log(LogLevel.Fatal, message, group, stack);
|
|
73
|
-
}
|
|
74
|
-
async onCatchError(error: Error | unknown, group?: string)
|
|
75
|
-
{
|
|
76
|
-
await this.error(this.getErrorMessage(error), group, this.getErrorStack(error));
|
|
77
|
-
}
|
|
78
|
-
async onCatchCritical(error: Error | unknown, group?: string)
|
|
79
|
-
{
|
|
80
|
-
await this.critical(this.getErrorMessage(error), group, this.getErrorStack(error));
|
|
81
|
-
}
|
|
82
|
-
async onCatchFatal(error: Error | unknown, group?: string)
|
|
83
|
-
{
|
|
84
|
-
await this.fatal(this.getErrorMessage(error), group, this.getErrorStack(error));
|
|
85
|
-
}
|
|
86
|
-
protected getErrorMessage(error: Error | unknown): string
|
|
87
|
-
{
|
|
88
|
-
if (error instanceof Error)
|
|
89
|
-
return error.message;
|
|
90
|
-
return error + "";
|
|
91
|
-
}
|
|
92
|
-
protected getErrorStack(error: Error | unknown): string
|
|
93
|
-
{
|
|
94
|
-
if (error instanceof Error)
|
|
95
|
-
return error.stack ?? "";
|
|
96
|
-
return error + "";
|
|
97
|
-
}
|
|
98
|
-
initUncought()
|
|
99
|
-
{
|
|
100
|
-
}
|
|
1
|
+
import { FormatterFull } from "./FormatterFull";
|
|
2
|
+
import { IFormatter } from "./IFormatter";
|
|
3
|
+
import { ILogger } from "./ILogger";
|
|
4
|
+
import { IStream } from "./IStream";
|
|
5
|
+
import { Log } from "./Log";
|
|
6
|
+
import { LogLevel } from "./LogLevel";
|
|
7
|
+
import { StreamConsole } from "./StreamConsole";
|
|
8
|
+
|
|
9
|
+
export class Logger implements ILogger
|
|
10
|
+
{
|
|
11
|
+
private defaultStream: IStream = new StreamConsole();
|
|
12
|
+
private formatter?: IFormatter = new FormatterFull();
|
|
13
|
+
private customFormatter: { [level: string]: IFormatter } = {};
|
|
14
|
+
private streams: IStream[] = [];
|
|
15
|
+
setFromatter(formatter: IFormatter): void
|
|
16
|
+
{
|
|
17
|
+
this.formatter = formatter;
|
|
18
|
+
}
|
|
19
|
+
setCustomFormatter(level: LogLevel, formatter: IFormatter): void
|
|
20
|
+
{
|
|
21
|
+
this.customFormatter[level] = formatter;
|
|
22
|
+
}
|
|
23
|
+
addStream(stream: IStream)
|
|
24
|
+
{
|
|
25
|
+
this.streams.push(stream);
|
|
26
|
+
}
|
|
27
|
+
async log(level: LogLevel, message: string, group?: string, stack?: string)
|
|
28
|
+
{
|
|
29
|
+
let log = new Log(level, message, group, stack);
|
|
30
|
+
let formatted: string[] = [];
|
|
31
|
+
let formatter = this.customFormatter[level.toString()] ?? this.formatter;
|
|
32
|
+
if (formatter)
|
|
33
|
+
formatted = await formatter.format(log);
|
|
34
|
+
if (this.streams.length == 0)
|
|
35
|
+
this.defaultStream.write(log, formatted);
|
|
36
|
+
else
|
|
37
|
+
{
|
|
38
|
+
let ps = this.streams.map(s => s.write(log, formatted));
|
|
39
|
+
await Promise.all(ps);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async trace(message: string, group?: string, stack?: string)
|
|
43
|
+
{
|
|
44
|
+
await this.log(LogLevel.Trace, message, group, stack);
|
|
45
|
+
}
|
|
46
|
+
async verbose(message: string, group?: string, stack?: string)
|
|
47
|
+
{
|
|
48
|
+
await this.log(LogLevel.Verbose, message, group, stack);
|
|
49
|
+
}
|
|
50
|
+
async debug(message: string, group?: string, stack?: string)
|
|
51
|
+
{
|
|
52
|
+
await this.log(LogLevel.Debug, message, group, stack);
|
|
53
|
+
}
|
|
54
|
+
async info(message: string, group?: string, stack?: string)
|
|
55
|
+
{
|
|
56
|
+
await this.log(LogLevel.Info, message, group, stack);
|
|
57
|
+
}
|
|
58
|
+
async warning(message: string, group?: string, stack?: string)
|
|
59
|
+
{
|
|
60
|
+
await this.log(LogLevel.Warning, message, group, stack);
|
|
61
|
+
}
|
|
62
|
+
async error(message: string, group?: string, stack?: string)
|
|
63
|
+
{
|
|
64
|
+
await this.log(LogLevel.Error, message, group, stack);
|
|
65
|
+
}
|
|
66
|
+
async critical(message: string, group?: string, stack?: string)
|
|
67
|
+
{
|
|
68
|
+
await this.log(LogLevel.Critical, message, group, stack);
|
|
69
|
+
}
|
|
70
|
+
async fatal(message: string, group?: string, stack?: string)
|
|
71
|
+
{
|
|
72
|
+
await this.log(LogLevel.Fatal, message, group, stack);
|
|
73
|
+
}
|
|
74
|
+
async onCatchError(error: Error | unknown, group?: string)
|
|
75
|
+
{
|
|
76
|
+
await this.error(this.getErrorMessage(error), group, this.getErrorStack(error));
|
|
77
|
+
}
|
|
78
|
+
async onCatchCritical(error: Error | unknown, group?: string)
|
|
79
|
+
{
|
|
80
|
+
await this.critical(this.getErrorMessage(error), group, this.getErrorStack(error));
|
|
81
|
+
}
|
|
82
|
+
async onCatchFatal(error: Error | unknown, group?: string)
|
|
83
|
+
{
|
|
84
|
+
await this.fatal(this.getErrorMessage(error), group, this.getErrorStack(error));
|
|
85
|
+
}
|
|
86
|
+
protected getErrorMessage(error: Error | unknown): string
|
|
87
|
+
{
|
|
88
|
+
if (error instanceof Error)
|
|
89
|
+
return error.message;
|
|
90
|
+
return error + "";
|
|
91
|
+
}
|
|
92
|
+
protected getErrorStack(error: Error | unknown): string
|
|
93
|
+
{
|
|
94
|
+
if (error instanceof Error)
|
|
95
|
+
return error.stack ?? "";
|
|
96
|
+
return error + "";
|
|
97
|
+
}
|
|
98
|
+
initUncought()
|
|
99
|
+
{
|
|
100
|
+
}
|
|
101
101
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export class NamirasoftLogServer
|
|
2
|
-
{
|
|
1
|
+
export class NamirasoftLogServer
|
|
2
|
+
{
|
|
3
3
|
}
|
package/src/StreamConsole.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { IStream } from "./IStream";
|
|
2
|
-
import { Log } from "./Log";
|
|
3
|
-
import { LogLevel } from './LogLevel';
|
|
4
|
-
|
|
5
|
-
export class StreamConsole implements IStream
|
|
6
|
-
{
|
|
7
|
-
async write(log: Log, formated: string[]): Promise<void>
|
|
8
|
-
{
|
|
9
|
-
formated.forEach(f =>
|
|
10
|
-
{
|
|
11
|
-
if (log.level == LogLevel.Trace)
|
|
12
|
-
console.trace(f);
|
|
13
|
-
else if (log.level == LogLevel.Verbose)
|
|
14
|
-
console.log(f);
|
|
15
|
-
else if (log.level == LogLevel.Debug)
|
|
16
|
-
console.debug(f);
|
|
17
|
-
else if (log.level == LogLevel.Info)
|
|
18
|
-
console.info(f);
|
|
19
|
-
else if (log.level == LogLevel.Warning)
|
|
20
|
-
console.warn(f);
|
|
21
|
-
else if (log.level == LogLevel.Error)
|
|
22
|
-
console.error(f);
|
|
23
|
-
else if (log.level == LogLevel.Critical)
|
|
24
|
-
console.error(f);
|
|
25
|
-
else if (log.level == LogLevel.Fatal)
|
|
26
|
-
console.error(f);
|
|
27
|
-
else
|
|
28
|
-
console.log(f);
|
|
29
|
-
})
|
|
30
|
-
}
|
|
1
|
+
import { IStream } from "./IStream";
|
|
2
|
+
import { Log } from "./Log";
|
|
3
|
+
import { LogLevel } from './LogLevel';
|
|
4
|
+
|
|
5
|
+
export class StreamConsole implements IStream
|
|
6
|
+
{
|
|
7
|
+
async write(log: Log, formated: string[]): Promise<void>
|
|
8
|
+
{
|
|
9
|
+
formated.forEach(f =>
|
|
10
|
+
{
|
|
11
|
+
if (log.level == LogLevel.Trace)
|
|
12
|
+
console.trace(f);
|
|
13
|
+
else if (log.level == LogLevel.Verbose)
|
|
14
|
+
console.log(f);
|
|
15
|
+
else if (log.level == LogLevel.Debug)
|
|
16
|
+
console.debug(f);
|
|
17
|
+
else if (log.level == LogLevel.Info)
|
|
18
|
+
console.info(f);
|
|
19
|
+
else if (log.level == LogLevel.Warning)
|
|
20
|
+
console.warn(f);
|
|
21
|
+
else if (log.level == LogLevel.Error)
|
|
22
|
+
console.error(f);
|
|
23
|
+
else if (log.level == LogLevel.Critical)
|
|
24
|
+
console.error(f);
|
|
25
|
+
else if (log.level == LogLevel.Fatal)
|
|
26
|
+
console.error(f);
|
|
27
|
+
else
|
|
28
|
+
console.log(f);
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
31
|
}
|
package/src/StreamFile.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import { IStream } from "./IStream";
|
|
4
|
-
import { Log } from "./Log";
|
|
5
|
-
|
|
6
|
-
export class StreamFile implements IStream
|
|
7
|
-
{
|
|
8
|
-
private baseName: string;
|
|
9
|
-
private basePath: string;
|
|
10
|
-
seprateByDate: boolean = true;
|
|
11
|
-
seprateByLevel: boolean = true;
|
|
12
|
-
seprateByGroup: boolean = false;
|
|
13
|
-
constructor(basePath: string = "./")
|
|
14
|
-
{
|
|
15
|
-
this.basePath = basePath;
|
|
16
|
-
this.baseName = this.getPackageName();
|
|
17
|
-
}
|
|
18
|
-
private getPackageName()
|
|
19
|
-
{
|
|
20
|
-
let pkgPath = path.resolve(process.cwd(), "");
|
|
21
|
-
if (!pkgPath.endsWith('package.json'))
|
|
22
|
-
{
|
|
23
|
-
pkgPath = path.join(pkgPath, 'package.json');
|
|
24
|
-
}
|
|
25
|
-
if (fs.existsSync(pkgPath))
|
|
26
|
-
{
|
|
27
|
-
let json = fs.readFileSync(pkgPath) + "";
|
|
28
|
-
let pkg = JSON.parse(json);
|
|
29
|
-
return pkg.name;
|
|
30
|
-
}
|
|
31
|
-
return "";
|
|
32
|
-
}
|
|
33
|
-
private getFilePath(log: Log): string
|
|
34
|
-
{
|
|
35
|
-
let names = [this.baseName];
|
|
36
|
-
if (this.seprateByDate)
|
|
37
|
-
names.push(log.date.getFullYear() + "-" + (log.date.getMonth() + 1) + "-" + log.date.getDate());
|
|
38
|
-
if (this.seprateByGroup)
|
|
39
|
-
if (log.group)
|
|
40
|
-
names.push(log.group);
|
|
41
|
-
if (this.seprateByLevel)
|
|
42
|
-
names.push(log.level + "");
|
|
43
|
-
names.push(".log");
|
|
44
|
-
let name = names.join("-");
|
|
45
|
-
return path.resolve(this.basePath, name);
|
|
46
|
-
}
|
|
47
|
-
async write(log: Log, formated: string[]): Promise<void>
|
|
48
|
-
{
|
|
49
|
-
let filePath = this.getFilePath(log);
|
|
50
|
-
fs.appendFile(filePath, formated.join("\n"), () => { });
|
|
51
|
-
}
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { IStream } from "./IStream";
|
|
4
|
+
import { Log } from "./Log";
|
|
5
|
+
|
|
6
|
+
export class StreamFile implements IStream
|
|
7
|
+
{
|
|
8
|
+
private baseName: string;
|
|
9
|
+
private basePath: string;
|
|
10
|
+
seprateByDate: boolean = true;
|
|
11
|
+
seprateByLevel: boolean = true;
|
|
12
|
+
seprateByGroup: boolean = false;
|
|
13
|
+
constructor(basePath: string = "./")
|
|
14
|
+
{
|
|
15
|
+
this.basePath = basePath;
|
|
16
|
+
this.baseName = this.getPackageName();
|
|
17
|
+
}
|
|
18
|
+
private getPackageName()
|
|
19
|
+
{
|
|
20
|
+
let pkgPath = path.resolve(process.cwd(), "");
|
|
21
|
+
if (!pkgPath.endsWith('package.json'))
|
|
22
|
+
{
|
|
23
|
+
pkgPath = path.join(pkgPath, 'package.json');
|
|
24
|
+
}
|
|
25
|
+
if (fs.existsSync(pkgPath))
|
|
26
|
+
{
|
|
27
|
+
let json = fs.readFileSync(pkgPath) + "";
|
|
28
|
+
let pkg = JSON.parse(json);
|
|
29
|
+
return pkg.name;
|
|
30
|
+
}
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
private getFilePath(log: Log): string
|
|
34
|
+
{
|
|
35
|
+
let names = [this.baseName];
|
|
36
|
+
if (this.seprateByDate)
|
|
37
|
+
names.push(log.date.getFullYear() + "-" + (log.date.getMonth() + 1) + "-" + log.date.getDate());
|
|
38
|
+
if (this.seprateByGroup)
|
|
39
|
+
if (log.group)
|
|
40
|
+
names.push(log.group);
|
|
41
|
+
if (this.seprateByLevel)
|
|
42
|
+
names.push(log.level + "");
|
|
43
|
+
names.push(".log");
|
|
44
|
+
let name = names.join("-");
|
|
45
|
+
return path.resolve(this.basePath, name);
|
|
46
|
+
}
|
|
47
|
+
async write(log: Log, formated: string[]): Promise<void>
|
|
48
|
+
{
|
|
49
|
+
let filePath = this.getFilePath(log);
|
|
50
|
+
fs.appendFile(filePath, formated.join("\n"), () => { });
|
|
51
|
+
}
|
|
52
52
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { IStream } from "./IStream";
|
|
2
|
-
import { Log } from "./Log";
|
|
3
|
-
|
|
4
|
-
export class StreamNamirasoftLog implements IStream
|
|
5
|
-
{
|
|
6
|
-
token: string;
|
|
7
|
-
constructor(token: string)
|
|
8
|
-
{
|
|
9
|
-
this.token = token;
|
|
10
|
-
}
|
|
11
|
-
async write(_: Log, __: string[]): Promise<void>
|
|
12
|
-
{
|
|
13
|
-
}
|
|
1
|
+
import { IStream } from "./IStream";
|
|
2
|
+
import { Log } from "./Log";
|
|
3
|
+
|
|
4
|
+
export class StreamNamirasoftLog implements IStream
|
|
5
|
+
{
|
|
6
|
+
token: string;
|
|
7
|
+
constructor(token: string)
|
|
8
|
+
{
|
|
9
|
+
this.token = token;
|
|
10
|
+
}
|
|
11
|
+
async write(_: Log, __: string[]): Promise<void>
|
|
12
|
+
{
|
|
13
|
+
}
|
|
14
14
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export * from "./BaseFormatter";
|
|
2
|
-
export * from "./FormatterFull";
|
|
3
|
-
export * from "./FormatterShort";
|
|
4
|
-
export * from "./IFormatter";
|
|
5
|
-
export * from "./ILogger";
|
|
6
|
-
export * from "./IStream";
|
|
7
|
-
export * from "./Log";
|
|
8
|
-
export * from "./Logger";
|
|
9
|
-
export * from "./LogLevel";
|
|
10
|
-
export * from "./NamirasoftLogServer";
|
|
11
|
-
export * from "./StreamConsole";
|
|
12
|
-
export * from "./StreamFile";
|
|
1
|
+
export * from "./BaseFormatter";
|
|
2
|
+
export * from "./FormatterFull";
|
|
3
|
+
export * from "./FormatterShort";
|
|
4
|
+
export * from "./IFormatter";
|
|
5
|
+
export * from "./ILogger";
|
|
6
|
+
export * from "./IStream";
|
|
7
|
+
export * from "./Log";
|
|
8
|
+
export * from "./Logger";
|
|
9
|
+
export * from "./LogLevel";
|
|
10
|
+
export * from "./NamirasoftLogServer";
|
|
11
|
+
export * from "./StreamConsole";
|
|
12
|
+
export * from "./StreamFile";
|
|
13
13
|
export * from "./StreamNamirasoftLog";
|
package/tsconfig.json
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES6",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"rootDir": "./src",
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"lib": [
|
|
8
|
-
"es6",
|
|
9
|
-
"dom"
|
|
10
|
-
],
|
|
11
|
-
"allowJs": false,
|
|
12
|
-
"checkJs": false,
|
|
13
|
-
"strict": true,
|
|
14
|
-
"esModuleInterop": true,
|
|
15
|
-
"sourceMap": true,
|
|
16
|
-
"resolveJsonModule": true,
|
|
17
|
-
"declaration": true,
|
|
18
|
-
"noUnusedLocals": true,
|
|
19
|
-
"noUnusedParameters": true,
|
|
20
|
-
"noImplicitAny": true,
|
|
21
|
-
"noImplicitOverride": true,
|
|
22
|
-
"noImplicitReturns": true,
|
|
23
|
-
"noImplicitThis": true,
|
|
24
|
-
"skipLibCheck": false,
|
|
25
|
-
"allowSyntheticDefaultImports": true,
|
|
26
|
-
"forceConsistentCasingInFileNames": true,
|
|
27
|
-
"noFallthroughCasesInSwitch": true,
|
|
28
|
-
"isolatedModules": false,
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"lib": [
|
|
8
|
+
"es6",
|
|
9
|
+
"dom"
|
|
10
|
+
],
|
|
11
|
+
"allowJs": false,
|
|
12
|
+
"checkJs": false,
|
|
13
|
+
"strict": true,
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"noUnusedLocals": true,
|
|
19
|
+
"noUnusedParameters": true,
|
|
20
|
+
"noImplicitAny": true,
|
|
21
|
+
"noImplicitOverride": true,
|
|
22
|
+
"noImplicitReturns": true,
|
|
23
|
+
"noImplicitThis": true,
|
|
24
|
+
"skipLibCheck": false,
|
|
25
|
+
"allowSyntheticDefaultImports": true,
|
|
26
|
+
"forceConsistentCasingInFileNames": true,
|
|
27
|
+
"noFallthroughCasesInSwitch": true,
|
|
28
|
+
"isolatedModules": false,
|
|
29
|
+
}
|
|
30
30
|
}
|