Skip to content
This repository was archived by the owner on Feb 2, 2018. It is now read-only.

Commit f27e494

Browse files
committed
docs: add readme
1 parent 27c2063 commit f27e494

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/logger-mixin/mixins/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Mixins
2+
3+
This directory contains mixins for `Application`.
4+
5+
## Overview
6+
7+
A mixin is a class which take the input of a base class and adds / modifies existing functions / status values and returns a new class which can be instantiated. The type of the returned class will still be the base class. Mixins let you extend the BaseClass by adding more functions to it, overriding existing ones or modifiying their behavior.
8+
9+
## Basic Usage
10+
11+
The general idea behind using a mixin is to pass in the BaseClass to the Mixin and create a new class.
12+
13+
**Example**
14+
```
15+
cosnt newClass = MixinClass(BaseClass);
16+
```
17+
18+
Mixins can be nested, as such you can do the following:
19+
```
20+
cosnt newClass = MixinClass1(MixinClass2(BaseClass))
21+
```
22+
23+
### LoggerMixin
24+
25+
LoggerMixin adds capabilities to a LoopBack Next Application by adding a `.logger()` function that allows used to bind a Logger class to `Context` automatically. The binding key will be `loggers.${Class.name}` where `Class.name` is the name of the Logger class being bound. The Mixin also overrides existing `.component()` function so that components are also capable of providing Logger's to be bound automatically.
26+
27+
**Example**
28+
```
29+
const LoggingApplication = LoggerMixin(Application); // we mixin the Application class from @loopback/core
30+
31+
const app = new LoggingApplication({
32+
loggers: [ColorLogger] // we can provide an array of loggers to bind automatically at startup
33+
});
34+
35+
// Example Logger
36+
class ColorLogger {
37+
log(...args: any[]) {
38+
console.log('log :', ...args);
39+
}
40+
41+
info(...args: any[]) {
42+
const data = args.join(' ');
43+
console.log('\x1b[32m info : ' + data + '\x1b[0m');
44+
}
45+
46+
warn(...args: any[]) {
47+
const data = args.join(' ');
48+
console.log('\x1b[33m warn : ' + data + '\x1b[0m');
49+
}
50+
51+
error(...args: any[]) {
52+
const data = args.join(' ');
53+
console.log('\x1b[31m error: ' + data + '\x1b[0m');
54+
}
55+
};
56+
```
57+
58+
Once a Logger has been bound, you can retrieve it by using [Dependency Inject](http://loopback.io/doc/en/lb4/Dependency-injection.html)
59+
60+
**More Examples for binding a Logger**
61+
```
62+
// Using the app's .logger() function.
63+
class LoggingApplication extends LoggerMixin(Application) {
64+
constructor() {
65+
super();
66+
const app = this;
67+
}
68+
69+
app.logger(ColorLogger);
70+
}
71+
72+
// Binding a Logger provided by a components
73+
class MyComponent {
74+
loggers: [ColorLogger];
75+
}
76+
77+
const LoggingApplication = LoggerMixin(Application);
78+
79+
const app = new LoggingApplication({
80+
components: [MyComponent] // Logger from MyComponent will be bound to loggers.ColorLogger
81+
});
82+
```
83+
84+
## Contributions
85+
86+
- [Guidelines](http://loopback.io/doc/en/contrib/index.html)
87+
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)
88+
89+
## Tests
90+
91+
Run `npm test` from the root folder.
92+
93+
## Contributors
94+
95+
See [all contributors](https://github.com/strongloop/loopback-next-extension-starter/graphs/contributors).
96+
97+
## License
98+
99+
MIT

0 commit comments

Comments
 (0)