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

Commit

Permalink
docs: add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
virkt25 committed Sep 29, 2017
1 parent 27c2063 commit f27e494
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/logger-mixin/mixins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Mixins

This directory contains mixins for `Application`.

## Overview

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.

## Basic Usage

The general idea behind using a mixin is to pass in the BaseClass to the Mixin and create a new class.

**Example**
```
cosnt newClass = MixinClass(BaseClass);
```

Mixins can be nested, as such you can do the following:
```
cosnt newClass = MixinClass1(MixinClass2(BaseClass))
```

### LoggerMixin

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.

**Example**
```
const LoggingApplication = LoggerMixin(Application); // we mixin the Application class from @loopback/core
const app = new LoggingApplication({
loggers: [ColorLogger] // we can provide an array of loggers to bind automatically at startup
});
// Example Logger
class ColorLogger {
log(...args: any[]) {
console.log('log :', ...args);
}
info(...args: any[]) {
const data = args.join(' ');
console.log('\x1b[32m info : ' + data + '\x1b[0m');
}
warn(...args: any[]) {
const data = args.join(' ');
console.log('\x1b[33m warn : ' + data + '\x1b[0m');
}
error(...args: any[]) {
const data = args.join(' ');
console.log('\x1b[31m error: ' + data + '\x1b[0m');
}
};
```

Once a Logger has been bound, you can retrieve it by using [Dependency Inject](http://loopback.io/doc/en/lb4/Dependency-injection.html)

**More Examples for binding a Logger**
```
// Using the app's .logger() function.
class LoggingApplication extends LoggerMixin(Application) {
constructor() {
super();
const app = this;
}
app.logger(ColorLogger);
}
// Binding a Logger provided by a components
class MyComponent {
loggers: [ColorLogger];
}
const LoggingApplication = LoggerMixin(Application);
const app = new LoggingApplication({
components: [MyComponent] // Logger from MyComponent will be bound to loggers.ColorLogger
});
```

## Contributions

- [Guidelines](http://loopback.io/doc/en/contrib/index.html)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See [all contributors](https://github.com/strongloop/loopback-next-extension-starter/graphs/contributors).

## License

MIT

0 comments on commit f27e494

Please sign in to comment.