Skip to content

Commit 12f0a4c

Browse files
committed
Merge stmp
2 parents ea81dd0 + e889952 commit 12f0a4c

File tree

15 files changed

+497
-454
lines changed

15 files changed

+497
-454
lines changed

docs/.vuepress/config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ module.exports = {
4747
{ link: '/appenders/file.html', text: 'File', items: [] },
4848
{ link: '/appenders/file-date.html', text: 'File date', items: [] },
4949
{ link: '/appenders/stdout.html', text: 'Stdout', items: [] },
50-
{ link: '/appenders/stderr.html', text: 'Stderr', items: [] }
50+
{ link: '/appenders/stderr.html', text: 'Stderr', items: [] },
51+
{ link: '/appenders/smtp.html', text: 'Smtp', items: [] }
5152
]
5253
},
5354
{
@@ -100,7 +101,8 @@ module.exports = {
100101
'file',
101102
'file-date',
102103
'stdout',
103-
'stderr'
104+
'stderr',
105+
'smtp'
104106
]
105107
},
106108
{
@@ -146,6 +148,7 @@ module.exports = {
146148
'/appenders/file-date',
147149
'/appenders/stdout',
148150
'/appenders/stderr',
151+
'/appenders/smtp',
149152
'/layouts/basic',
150153
'/layouts/colored',
151154
'/layouts/dummy',

docs/appenders/file.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ implementation (see also node.js core file streams):
2424
- **compress** - boolean (default false) - compress the backup files during rolling (backup files will have .gz extension)
2525

2626
## Example
27+
2728
```typescript
2829
import {Logger} from "@tsed/logger";
2930

docs/appenders/smtp.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# SMTP Appender
2+
3+
Sends log events as emails. If you use this appender, you should also call `logger.shutdown` when your application closes so that any remaining emails can be sent. Many of the configuration options below are passed through to nodemailer, so you should read their docs to get the most out of this appender.
4+
5+
```bash
6+
npm install @tsed/logger
7+
npm install @tsed/logger-smtp
8+
```
9+
10+
## Configuration
11+
12+
* `type` - `smtp`
13+
* `SMTP` - `object` (optional, if not present will use `transport` field)
14+
* `host` - `string` (optional, defaults to `localhost`)
15+
* `port` - `integer` (optional, defaults to `25`)
16+
* `auth` - `object` (optional) - authentication details
17+
* `user` - `string`
18+
* `pass` - `string`
19+
* `transport` - `object` (optional, if not present will use `SMTP`) - see [`nodemailer`](https://nodemailer.com/smtp/) docs for transport options
20+
* `plugin` - `string` (optional, defaults to `smtp`) - the nodemailer transport plugin to use
21+
* `options` - `object` - configuration for the transport plugin
22+
* `attachment` - `object` (optional) - send logs as email attachment
23+
* `enable` - `boolean` (optional, defaults to `false`)
24+
* `message` - `string` (optional, defaults to `See logs as attachment`) - message to put in body of email
25+
* `filename` - `string` (optional, defaults to `default.log`) - attachment filename
26+
* `sendInterval` - `integer` (optional, defaults to `0`) - batch emails and send in one email every `sendInterval` seconds, if `0` then every log message will send an email.
27+
* `shutdownTimeout` - `integer` (optional, defaults to `5`) - time in seconds to wait for emails to be sent during shutdown
28+
* `recipients` - `string` - email addresses to send the logs to
29+
* `subject` - `string` (optional, defaults to message from first log event in batch) - subject for email
30+
* `sender` - `string` (optional) - who the logs should be sent as
31+
* `html` - `boolean` (optional, defaults to `false`) - send the email as HTML instead of plain text
32+
* `layout` - `object` (optional, defaults to basicLayout) - see [layouts](https://logger.tsed.io/layouts.html)
33+
* `cc` - `string` (optional) - email addresses to send the carbon-copy logs to
34+
* `bcc` - `string` (optional) - email addresses to send the blind-carbon-copy logs to
35+
36+
## Example (default config)
37+
38+
```typescript
39+
import {Logger} from "@tsed/logger";
40+
import "@tsed/logger-smtp"
41+
42+
const logger = new Logger("loggerName");
43+
44+
logger.appenders.set("email", {
45+
type: "smtp",
46+
level: ["error"],
47+
recipients: "dev.team@company.name"
48+
});
49+
```
50+
51+
This configuration will send an email using the smtp server running on `localhost:25`, for every log event of level `ERROR` and above.
52+
The email will be sent to `dev.team@company.name`, the subject will be the message part of the log event, the body of the email will be log event formatted by the basic layout function.
53+
54+
## Example (logs as attachments, batched)
55+
56+
```typescript
57+
import {Logger} from "@tsed/logger";
58+
import "@tsed/logger-smtp"
59+
60+
const logger = new Logger("loggerName");
61+
62+
logger.appenders.set("email", {
63+
type: "smtp",
64+
level: ["error"],
65+
recipients: "dev.team@company.name",
66+
subject: "Latest logs",
67+
sender: "my.application@company.name",
68+
attachment: {
69+
enable: true,
70+
filename: "latest.log",
71+
message: "See the attachment for the latest logs"
72+
},
73+
sendInterval: 3600
74+
});
75+
```
76+
77+
This configuration will send an email once every hour, with all the log events of level `ERROR` and above as an attached file.
78+
79+
## Example (custom SMTP host)
80+
81+
```javascript
82+
import {Logger} from "@tsed/logger";
83+
import "@tsed/logger-smtp"
84+
85+
const logger = new Logger("loggerName");
86+
87+
logger.appenders.set("email", {
88+
type: "smtp",
89+
level: ["error"],
90+
recipients: "dev.team@company.name",
91+
SMTP: { host: 'smtp.company.name', port: 8025 }
92+
});
93+
```
94+
95+
This configuration can also be written as:
96+
97+
```javascript
98+
import {Logger} from "@tsed/logger";
99+
import "@tsed/logger-smtp"
100+
101+
const logger = new Logger("loggerName");
102+
103+
logger.appenders.set("email", {
104+
type: "smtp",
105+
level: ["error"],
106+
recipients: "dev.team@company.name",
107+
transport: {
108+
plugin: 'smtp',
109+
options: {
110+
host: 'smtp.company.name',
111+
port: 8025
112+
}
113+
}
114+
});
115+
```
116+
A similar config can be used to specify a different transport plugin than `smtp`. See the [`nodemailer`](https://nodemailer.com/smtp/) docs for more details.

packages/logger/src/appenders/decorators/appender.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {IAppenderOptions} from "../class/BaseAppender";
88
export function Appender(options: IAppenderOptions) {
99
return (target: any) => {
1010
target.prototype.appenderOptions = options;
11+
target.$name = options.name;
1112
AppendersRegistry.set(options.name, {provide: target});
1213
};
1314
}
Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,8 @@
1-
/**
2-
* ## Appenders
3-
*
4-
* Appenders serialise log events to some form of output. They can write to files, send emails, send data over the network. All appenders have a type which determines which appender gets used. For example:
5-
*
6-
* ```typescript
7-
* import {Logger} from "ts-log-debug";
8-
*
9-
* const logger = new Logger("loggerName");
10-
*
11-
* logger.appenders
12-
* .push({
13-
* type: "stdout", level: ["debug", "info", "trace"]
14-
* })
15-
* .push({
16-
* type: "stderr", level: ["error", "fatal", "warn"]
17-
* });
18-
* .push({
19-
* type: "file", filename: "logfile.log"
20-
* });
21-
* ```
22-
*
23-
* This defines three appenders named ‘stdout’, ‘stderr’ and ‘file’.
24-
*
25-
* ### Core Appenders
26-
*
27-
* The following appenders are included with ts-log-debug.
28-
*
29-
* * console
30-
* * file
31-
* * stderr
32-
* * stdout
33-
*
34-
* ### Custom Appender
35-
*
36-
* ts-log-debug can load appenders from outside the core appenders. The type config value is used as a require path if no matching appender can be found. For example, the following configuration will create an appender with decorators:
37-
*
38-
* ```typescript
39-
* // consoleAppender.ts
40-
* import {Appender, BaseAppender, LogEvent} from "ts-log-debug";
41-
* const consoleLog = console.log.bind(console);
42-
*
43-
* &arobase;Appender({name: "console2"})
44-
* export class ConsoleAppender extends BaseAppender {
45-
* write(loggingEvent: LogEvent) {
46-
* consoleLog(this.layout(loggingEvent, this.config.timezoneOffset));
47-
* }
48-
* }
49-
* ```
50-
*
51-
* This appender can be use like this:
52-
*
53-
* ```typescript
54-
* import {Logger} from "ts-log-debug";
55-
* import "./consoleAppender.ts"
56-
*
57-
* const logger = new Logger("loggerName");
58-
*
59-
* logger.appenders
60-
* .push({
61-
* type: "console2", level: ["debug", "info", "trace"]
62-
* });
63-
* ```
64-
*
65-
* @module appenders
66-
* @preferred
67-
*/
68-
/** */
69-
import "./components/ConsoleAppender";
70-
import "./components/FileAppender";
71-
import "./components/StderrAppender";
72-
import "./components/StdoutAppender";
1+
export * from "./components/ConsoleAppender";
2+
export * from "./components/FileAppender";
3+
export * from "./components/StderrAppender";
4+
export * from "./components/StdoutAppender";
735

6+
export * from "./interfaces/AppenderConfiguration";
747
export * from "./class/BaseAppender";
758
export * from "./decorators/appender";

packages/logger/src/index.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
/**
2-
* @preferred
3-
*
4-
* @fileoverview ts-log-debug is a library to log in TypeScript.
5-
*
6-
* <h3>Example:</h3>
7-
* <pre>
8-
* import {$log} from "ts-log-debug";
9-
* $log.level = "debug";
10-
* $log.name = "APP";
11-
* $log.debug("Some debug messages");
12-
* </pre>
13-
*
14-
* @author Lenzotti Romain
15-
* @since 2017-06-18
16-
* @static
17-
* Website:
18-
*/
191
export * from "./core";
202
export * from "./appenders";
213
export * from "./layouts";

packages/logger/src/layouts/class/Layouts.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
/**
2-
* @module layouts
3-
*/
4-
/** */
51
import {LayoutsRegistry} from "../registries/LayoutsRegistry";
62
import {IBasicLayoutConfiguration} from "../interfaces/BasicLayoutConfiguration";
3+
import {BaseLayout} from "./BaseLayout";
74

85
export class Layouts {
9-
static get(name: string, config: IBasicLayoutConfiguration) {
6+
static get(name: string | any, config: IBasicLayoutConfiguration): BaseLayout {
7+
if (typeof name !== "string") {
8+
name = name.$name;
9+
}
10+
1011
if (!LayoutsRegistry.has(name)) {
1112
name = "colored";
1213
console.warn(name + " layout doesn't exists");
1314
}
1415

15-
const layoutKlass: any = LayoutsRegistry.get(name.replace(/layout/gi, ""));
16+
const layoutKlass: any = LayoutsRegistry.get(name);
1617

1718
return new layoutKlass.provide(config);
1819
}

packages/logger/src/layouts/decorators/layout.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {LayoutsRegistry} from "../registries/LayoutsRegistry";
22

33
export function Layout(options: {name: string}) {
44
return (target: any) => {
5+
target.$name = options.name;
56
LayoutsRegistry.set(options.name, {provide: target});
67
};
78
}

0 commit comments

Comments
 (0)