Skip to content

Commit d982ace

Browse files
authored
Merge pull request #9 from SecJS/refactor/len-driver-support
Refactor/len driver support
2 parents b9f6e3d + 4358479 commit d982ace

29 files changed

Lines changed: 847 additions & 1327 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ out
126126
*.d.ts
127127
dist
128128
build
129+
storage

README.md

Lines changed: 154 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,102 +31,196 @@ npm install @secjs/logger
3131

3232
## Usage
3333

34+
### Config logging template
35+
36+
> First you need to create the configuration file logging in the config folder on project root path. Is extremely important to use export default in these configurations.
37+
38+
```ts
39+
import { Env } from '@secjs/env'
40+
import { Path } from '@secjs/utils'
41+
42+
export default {
43+
/*
44+
|--------------------------------------------------------------------------
45+
| Default Log Channel
46+
|--------------------------------------------------------------------------
47+
|
48+
| This option defines the default log channel that gets used when writing
49+
| messages to the logs. The name specified in this option should match
50+
| one of the channels defined in the "channels" configuration object.
51+
|
52+
*/
53+
54+
default: Env('LOGGING_CHANNEL', 'application'),
55+
56+
/*
57+
|--------------------------------------------------------------------------
58+
| Log Channels
59+
|--------------------------------------------------------------------------
60+
|
61+
| Here you may configure the log channels for your application.
62+
|
63+
| Available Drivers: "console", "debug", "file".
64+
| Available Formatters: "context", "debug", "json", "log".
65+
|
66+
*/
67+
68+
channels: {
69+
application: {
70+
driver: 'console',
71+
context: 'Logger',
72+
formatter: 'context',
73+
},
74+
debug: {
75+
driver: 'debug',
76+
context: 'Debugger',
77+
formatter: 'context',
78+
namespace: 'api:main',
79+
},
80+
file: {
81+
driver: 'file',
82+
context: 'Logger',
83+
formatter: 'log',
84+
filePath: Path.noBuild().logs('secjs.log'),
85+
},
86+
},
87+
}
88+
```
89+
3490
### Log / Logger
3591

36-
> Log any type of requests in your application in public mode
92+
> With the config/logging file created you can use Log and Logger classes to start logging.
3793
3894
```ts
39-
import { Log, Logger } from '@secjs/logger'
95+
import { Log, Logger, Color } from '@secjs/logger'
4096

41-
Log('Hello World!')
42-
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Log] Hello World! +0ms
97+
// Log and Logger will always use the default values of channel inside config/logging, the default channel in here is "application".
98+
Log.log('Hello World!')
99+
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms
43100

44-
const logger = new Logger('LogService')
101+
const logger = new Logger()
45102

46103
logger.success('Hello World!')
47-
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [LogService] Hello World! +0ms
104+
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms
48105

49-
logger.warn('Hello World!', { context: 'LogController' })
106+
// You can pass options to formatters and drivers as second parameter
107+
logger.warn('Hello World!', { color: Color.purple, context: 'LogController' })
50108
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [LogController] Hello World! +0ms
51109
```
52110

53-
### Formatters / Transporters / LogMapper
111+
### Using other channels
54112

55-
> Use transporters and formatters to keep a pattern in how the application will handle the logs. And use mappers
56-
> to set formatters and transporters that are going to be used. See the example:
113+
> You can use any channel that you configure inside config/logging, SecJS has default channels inside the template file.
57114
58115
```ts
59-
import {
60-
LogMapper,
61-
JsonFormatter,
62-
FileTransporter,
63-
ContextFormatter,
64-
changeLogFnMapper,
65-
ConsoleTransporter,
66-
} from '@secjs/logger'
67-
68-
const logMapper = new LogMapper([new JsonFormatter()], [new FileTransporter()])
69-
70-
// This function is important to change the default mapper from Log function
71-
changeLogFnMapper(logMapper)
72-
const logger = new Logger('Context', logMapper)
73-
74-
// You can use addFormatter and addTransporter to add more formatters and transporters
75-
logMapper.addFormatter(new ContextFormatter('Context'))
76-
logMapper.addTransporter(new ConsoleTransporter('stdout'))
77-
78-
// You can use removeFormatter and removeTransporter too.
79-
logMapper.removeFormatter(ContextFormatter)
80-
logMapper.removeTransporter(ConsoleTransporter)
116+
Log.channel('debug').log('Hello debug world!', { namespace: 'api:example' })
117+
// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello debug world! +0ms
81118
```
82119

83-
> Now Log function and Logger class will use the logMapper instance,
84-
> all logs will be stringify by JsonFormatter, and be saved inside FileTransporter.
85-
86-
### Custom formatters and transporters
87-
88-
> Here are all the already implemented formatters and transporters from @secjs/logger
120+
> You can use many channels to handle the log in all of then
89121
90122
```ts
91-
import {
92-
LogFormatter,
93-
JsonFormatter,
94-
DebugFormatter,
95-
ContextFormatter,
96-
FileTransporter,
97-
DebugTransporter,
98-
ConsoleTransporter,
99-
} from '@secjs/logger'
123+
Log.channels('debug', 'application', 'file').info('Hello World!', { namespace: 'api:example' })
124+
// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello World! +0ms
125+
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms
126+
127+
// In storage/logs/secjs.log file
128+
// [SecJS] - PID: 196416 - dd/mm/yyyy, hh:mm:ss [INFO] Hello World!
100129
```
101130

102-
> You can define your own formatters and transporters using the contracts
131+
### Extending drivers, channels and formatters
132+
133+
> Nowadays, @secjs/logger has only FileDriver, DebugDriver and ConsoleDriver support, but you can extend the drivers for Logger class if you implement DriverContract interface.
103134
104135
```ts
105-
import { FormatterContract, TransporterContract } from '@secjs/logger'
136+
import { DriverContract, FormatterContract, format } from '@secjs/logger'
137+
138+
interface CustomDriverOpts {}
106139

107-
export class CustomFormatter implements FormatterContract {
108-
format(message: any, options?: any) {
109-
createFileToTransportToS3(message, options.filePath)
140+
class CustomDriver implements DriverContract {
141+
private readonly _level: string
142+
private readonly _context: string
143+
private readonly _formatter: string
144+
145+
constructor(channel: string) {
146+
const config = Config.get(`logging.channels.${channel}`)
110147

111-
deleteTheFile(options.filePath)
148+
this._level = config.level || 'INFO'
149+
this._context = config.context || 'CustomDriver'
150+
this._formatter = config.formatter || 'context'
151+
}
152+
153+
transport(message: string, options?: CustomDriverOpts): void {
154+
options = Object.assign(
155+
{},
156+
{
157+
level: this._level,
158+
context: this._context,
159+
streamType: this._streamType,
160+
},
161+
options,
162+
)
163+
164+
message = format(this._formatter, message, options)
165+
166+
process[this._streamType].write(`${message}\n`)
112167
}
113168
}
169+
```
170+
171+
> Same to extend formatters
172+
173+
```ts
174+
class CustomFormatter implements FormatterContract {
175+
// all the methods implemented from FormatterContract...
176+
}
177+
```
114178

115-
export class CustomTransporter implements TransporterContract {
116-
transport(logFormatted: any, options?: any) {
117-
sendToS3(logFormatted, options.s3Bucket)
179+
> Constructor is extremely important in your CustomDriver class, it's the constructor that will use the values from config/logging channels to manipulate your CustomDriver using channel and channels method from logger.
180+
> So if you are building a CustomDriver, and you want to use it, you can create a new channel inside config/logging channels or change the driver from an existing channel.
181+
182+
```ts
183+
// extending channels
184+
// config/logging file
185+
186+
export default {
187+
// default etc...
188+
189+
channels: {
190+
mychannel: {
191+
driver: 'custom',
192+
level: 'INFO',
193+
formatter: 'context',
194+
context: 'Logger',
195+
}
196+
// ... other disks
118197
}
119198
}
120199
```
121200

122-
> Then, use it with the mappers
201+
> Now you can build your new driver using Logger class
123202
124203
```ts
125-
import { LogMapper } from '@secjs/logger'
204+
const driverName = 'custom'
205+
const formatterName = 'custom'
206+
const driver = CustomDriver
207+
const formatter = CustomFormatter
126208

127-
const s3LogMapper = new LogMapper([new CustomFormatter()], [new CustomFormatter()])
209+
Logger.buildDriver(driverName, driver)
210+
Logger.buildFormatter(formatterName, CustomFormatter)
211+
212+
console.log(Logger.drivers) // ['console', 'debug', 'file', 'custom']
213+
console.log(Logger.formatters) // ['context', 'debug', 'json', 'log', 'custom']
214+
```
215+
216+
> Now, if you have implemented your channel in config/logging, you can use him inside logger
217+
218+
```ts
219+
// options of your driver and formatter
220+
const options = {}
128221

129-
const s3Logger = new Logger('S3LoggerService', s3LogMapper)
222+
// Will use CustomDriver to handle the log actions
223+
logger.channel('mychannel').success('Hello World!!', options)
130224
```
131225

132226
---

config/logging.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Env } from '@secjs/env'
2+
import { Path } from '@secjs/utils'
3+
4+
export default {
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Default Log Channel
8+
|--------------------------------------------------------------------------
9+
|
10+
| This option defines the default log channel that gets used when writing
11+
| messages to the logs. The name specified in this option should match
12+
| one of the channels defined in the "channels" configuration object.
13+
|
14+
*/
15+
16+
default: Env('LOGGING_CHANNEL', 'application'),
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Log Channels
21+
|--------------------------------------------------------------------------
22+
|
23+
| Here you may configure the log channels for your application.
24+
|
25+
| Available Drivers: "console", "debug", "file".
26+
| Available Formatters: "context", "debug", "json", "log".
27+
|
28+
*/
29+
30+
channels: {
31+
application: {
32+
driver: 'console',
33+
context: 'Logger',
34+
formatter: 'context',
35+
},
36+
debug: {
37+
driver: 'debug',
38+
context: 'Debugger',
39+
formatter: 'context',
40+
namespace: 'api:main',
41+
},
42+
file: {
43+
driver: 'file',
44+
context: 'Logger',
45+
formatter: 'log',
46+
filePath: Path.noBuild().logs('secjs.log'),
47+
},
48+
},
49+
}

index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
export * from './src/utils/Color'
2-
export * from './src/utils/getTimestamp'
3-
4-
export * from './src/Logger/Log'
5-
export * from './src/Logger/Logger'
6-
export * from './src/Logger/LogMapper'
7-
export * from './src/Logger/defaultMapper'
8-
1+
export * from './src/Contracts/DriverContract'
92
export * from './src/Contracts/FormatterContract'
10-
export * from './src/Contracts/TransporterContract'
3+
4+
export * from './src/Drivers/FileDriver'
5+
export * from './src/Drivers/DebugDriver'
6+
export * from './src/Drivers/ConsoleDriver'
117

128
export * from './src/Formatters/LogFormatter'
139
export * from './src/Formatters/JsonFormatter'
1410
export * from './src/Formatters/DebugFormatter'
1511
export * from './src/Formatters/ContextFormatter'
1612

17-
export * from './src/Transporters/FileTransporter'
18-
export * from './src/Transporters/DebugTransporter'
19-
export * from './src/Transporters/ConsoleTransporter'
13+
export * from './src/utils/Color'
14+
export * from './src/utils/format'
15+
export * from './src/utils/getTimestamp'
16+
17+
export * from './src/Log'
18+
export * from './src/Logger'

0 commit comments

Comments
 (0)