Skip to content

Commit 39238f9

Browse files
committed
Added a bundled system lib.
Added a bundled system lib. Added a dev env setup. Added a couple of demos.
1 parent ed39ebb commit 39238f9

18 files changed

+416
-259
lines changed

.gitignore

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ typings
1111
# Optional REPL history
1212
.node_repl_history
1313

14-
**/*.js
15-
**/*.js.map
14+
dist
15+
bundles
16+
17+
*.js
18+
*.js.map
19+
*.d.ts
20+
21+
app/**/*.js
22+
app/**/*.js.map
23+
app/**/*.d.ts
1624

1725
.idea/workspace.xml

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ typings
1515
.idea
1616
*.iml
1717

18+
demos
19+
1820
tsconfig.json
1921

22+
typings.json
23+
2024
.travis.yml

README.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,10 @@ Setup the Provider.
2626

2727
bootstrap( App, [ Logger ]);
2828

29-
Add `angular2-logger` to your `Systemjs` configuration.*
29+
Add the `angular2-logger` library to your app.
3030
If you are following the [Angular 2's Quickstart Guide](https://angular.io/docs/ts/latest/quickstart.html) it should be something like this:
3131

32-
System.config({
33-
map: { 'angular2-logger': 'node_modules/angular2-logger' },
34-
packages: {
35-
app: {
36-
format: 'register',
37-
defaultExtension: 'js'
38-
},
39-
'angular2-logger': {
40-
format: 'register',
41-
defaultExtension: 'js'
42-
}
43-
}
44-
});
32+
<script src="node_modules/angular2-logger/bundles/angular2-logger.js"></script>
4533

4634
Inject the logger into your objects and use it.
4735

@@ -155,17 +143,33 @@ Class names like `Options` and `Level` might be too common, if you get a conflic
155143
## How you can help
156144
Filing issues is helpful but **pull requests** that improve the docs are even better!
157145

146+
## Instructions for dev environment
147+
They are long so try to keep up, here we go:
148+
149+
git clone https://github.com/code-chunks/angular2-logger.git
150+
151+
cd angular2-logger
152+
153+
npm i
154+
155+
Done.
158156

159157
## TODOs
160-
- <del>Add an off level that turns off the logger</del>
161-
- Support different loaders and modes.
158+
- <del>Add a `Level.OFF` that turns off the logger</del>.
159+
- <del>Support different loaders and modes</del>.
160+
- <del>Add a basic demo.</del>
161+
- Minify dist files.
162162
- Ability to add logging time to the messages.
163163
- Lazy Logging.
164164
- Appenders.
165+
- Add polyfill for Object.assign.
165166
- Unit Testing.
166167
- Support named loggers.
168+
- Message Layout Feature.
167169
- No coding required Dashboard UI to handle loggers.
168170

171+
[comment]: <> ( TODO: Fix tsconfig.json when #5980 https://github.com/Microsoft/TypeScript/pull/5980 gets implemented. )
172+
169173
*This step is only temporary until future, hopefully soon, improvements.
170174

171175
## LICENSE

app/core/level.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Created by Langley on 3/13/2016.
3+
*/
4+
5+
/**
6+
* The available options to set the Level of the Logger.
7+
* See {@link Logger}.
8+
*/
9+
export enum Level { OFF = 0, ERROR = 1, WARN = 2, INFO = 3, DEBUG = 4, LOG = 5 }

app/core/logger.ts

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import {Injectable, Optional} from "angular2/core";
2+
import {Level} from "./level.ts";
3+
4+
/**
5+
* Logger options.
6+
* See {@link Logger}.
7+
*
8+
* level - How much detail you want to see in the logs, 1 being the less detailed, 5 being the most. Defaults to WARN.
9+
* global - Whether you want the created logger object to be exposed in the global scope. Defaults to true.
10+
* globalAs - The window's property name that will hold the logger object created. Defaults to 'logger'.
11+
* store - Whether you want the level config to be saved in the local storage so it doesn't get lost when you refresh. Defaults to false.
12+
* storeAs - The local storage key that will be used to save the level config if the store setting is true. Defaults to 'angular2.logger.level'.
13+
*
14+
* Created by Langley on 3/23/2016.
15+
*
16+
*/
17+
export class Options {
18+
level: Level;
19+
global: boolean;
20+
globalAs: string;
21+
store: boolean;
22+
storeAs: string;
23+
}
24+
25+
// Temporal until https://github.com/angular/angular/issues/7344 gets fixed.
26+
const DEFAULT_OPTIONS: Options = {
27+
level: Level.WARN,
28+
global: true,
29+
globalAs: "logger",
30+
store: false,
31+
storeAs: "angular2.logger.level"
32+
};
33+
34+
@Injectable()
35+
export class Logger {
36+
37+
private _level: Level;
38+
private _globalAs: string;
39+
private _store: boolean;
40+
private _storeAs: string;
41+
42+
Level = Level;
43+
44+
constructor( @Optional() options?: Options ) {
45+
46+
// Move this to the constructor definition when optional parameters are working with @Injectable: https://github.com/angular/angular/issues/7344
47+
let { level, global, globalAs, store, storeAs } = Object.assign( {}, DEFAULT_OPTIONS, options );
48+
49+
this._level = level;
50+
this._globalAs = globalAs;
51+
this._storeAs = storeAs;
52+
53+
global && this.global();
54+
55+
if ( store || this._loadLevel() ) this.store();
56+
57+
}
58+
59+
private _loadLevel = (): Level => localStorage.getItem( this._storeAs );
60+
private _storeLevel(level: Level) { localStorage[ this._storeAs ] = level; }
61+
62+
error(message?: any, ...optionalParams: any[]) {
63+
this.isErrorEnabled() && console.error( message, ...optionalParams );
64+
}
65+
66+
warn(message?: any, ...optionalParams: any[]) {
67+
this.isWarnEnabled() && console.warn( message, ...optionalParams );
68+
}
69+
70+
info(message?: any, ...optionalParams: any[]) {
71+
this.isInfoEnabled() && console.info( message, ...optionalParams );
72+
}
73+
74+
debug(message?: any, ...optionalParams: any[]) {
75+
this.isDebugEnabled() && console.debug( message, ...optionalParams );
76+
}
77+
78+
log(message?: any, ...optionalParams: any[]) {
79+
this.isLogEnabled() && console.log( message, ...optionalParams );
80+
}
81+
82+
global = () => window[this._globalAs] = this;
83+
84+
store(): Logger {
85+
86+
this._store = true;
87+
let storedLevel = this._loadLevel();
88+
if ( storedLevel ) { this._level = storedLevel; }
89+
else { this._storeLevel( this.level ); }
90+
91+
return this;
92+
93+
}
94+
95+
unstore(): Logger {
96+
this._store = false;
97+
localStorage.removeItem( this._storeAs );
98+
return this;
99+
}
100+
101+
isErrorEnabled = (): boolean => this.level >= Level.ERROR;
102+
isWarnEnabled = (): boolean => this.level >= Level.WARN;
103+
isInfoEnabled = (): boolean => this.level >= Level.INFO;
104+
isDebugEnabled = (): boolean => this.level >= Level.DEBUG;
105+
isLogEnabled = (): boolean => this.level >= Level.LOG;
106+
107+
get level(): Level { return this._level; }
108+
109+
set level(level: Level) {
110+
this._store && this._storeLevel(level);
111+
this._level = level;
112+
}
113+
114+
}

core.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {provide} from "angular2/core";
2+
import {Options, Logger} from "./app/core/logger";
3+
import {Level} from "./app/core/level";
4+
5+
/**
6+
* @module
7+
* @description
8+
* Public API.
9+
*/
10+
export * from "./app/core/level.ts";
11+
export * from "./app/core/logger";
12+
13+
/**
14+
* Custom Providers if the user wants to avoid some configuration for common scenarios.
15+
* @type {Provider|Logger[]}
16+
*/
17+
export const OFF_LOGGER_PROVIDERS: any[] = [ provide( Options, { useValue: { level: Level.OFF } } ), Logger ];
18+
export const ERROR_LOGGER_PROVIDERS: any[] = [ provide( Options, { useValue: { level: Level.ERROR } } ), Logger ];
19+
export const WARN_LOGGER_PROVIDERS: any[] = [ provide( Options, { useValue: { level: Level.WARN } } ), Logger ];
20+
export const INFO_LOGGER_PROVIDERS: any[] = [ provide( Options, { useValue: { level: Level.INFO } } ), Logger ];
21+
export const DEBUG_LOGGER_PROVIDERS: any[] = [ provide( Options, { useValue: { level: Level.DEBUG } } ), Logger ];
22+
export const LOG_LOGGER_PROVIDERS: any[] = [ provide( Options, { useValue: { level: Level.LOG } } ), Logger ];
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {Component} from 'angular2/core';
2+
import {Logger} from "angular2-logger/core";
3+
4+
@Component({
5+
selector: "logger-app",
6+
template: `
7+
8+
<h1>Logger's current priority level: {{logger.level}} </h1>
9+
10+
<h3>Change the Logger's priority level</h3>
11+
12+
<button (click)="setLevel(0)"> Off </button>
13+
<button (click)="setLevel(1)"> Error </button>
14+
<button (click)="setLevel(2)"> Warning </button>
15+
<button (click)="setLevel(3)"> Info </button>
16+
<button (click)="setLevel(4)"> Debug </button>
17+
<button (click)="setLevel(5)"> Log </button>
18+
19+
<h3>Watch their effects in the console</h3>
20+
21+
<button (click)="logMsgs()">Log Messages</button>
22+
<button (click)="clear()"> Clear console </button>
23+
24+
<h3>Store the logger's current level in the browser's local storage so it doesn't get reset after refreshing the page. </h3>
25+
<button (click)="store()"> store </button>
26+
<button (click)="unstore()"> unstore </button>
27+
28+
`
29+
})
30+
export class LoggerAppComponent{
31+
32+
constructor(public logger:Logger){}
33+
34+
logMsgs(){
35+
this.logger.error('This is a priority level 1 error message...');
36+
this.logger.warn('This is a priority level 2 warning message...');
37+
this.logger.info('This is a priority level 3 info message...');
38+
this.logger.debug('This is a priority level 4 debug message...');
39+
this.logger.log('This is a priority level 5 log message...');
40+
}
41+
42+
setLevel(level){
43+
this.logger.level = level;
44+
}
45+
46+
clear(){
47+
console.clear();
48+
}
49+
50+
store(){
51+
this.logger.store();
52+
}
53+
54+
unstore(){
55+
this.logger.unstore();
56+
}
57+
58+
}

demos/features/app/main.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {bootstrap} from 'angular2/platform/browser';
2+
import {LoggerAppComponent} from "./logger-app.component";
3+
import {Logger} from "angular2-logger/core";
4+
5+
bootstrap( LoggerAppComponent,[ Logger ]);

demos/features/index.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
5+
<meta charset="UTF-8">
6+
<title>Angular 2 Logger - Features </title>
7+
8+
<!-- 1. Load libraries -->
9+
<!-- IE required polyfills (from CDN), in this exact order -->
10+
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script>
11+
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.16/system-polyfills.js"></script>
12+
<script src="https://npmcdn.com/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
13+
<script src="https://code.angularjs.org/tools/system.js"></script>
14+
<script src="https://code.angularjs.org/tools/typescript.js"></script>
15+
<script src="https://code.angularjs.org/2.0.0-beta.12/angular2-polyfills.js"></script>
16+
<script src="https://code.angularjs.org/2.0.0-beta.12/Rx.js"></script>
17+
<script src="https://code.angularjs.org/2.0.0-beta.12/angular2.dev.js"></script>
18+
19+
<!-- 1.1 Add the angular2-logger library -->
20+
<script src="../../bundles/angular2-logger.js"></script>
21+
<!--In a real app use it like this:-->
22+
<!--<script src="node_modules/angular2-logger/bundles/angular2-logger.js"></script>-->
23+
24+
<!-- 2. Configure SystemJS -->
25+
<script>
26+
System.config({
27+
transpiler: 'typescript',
28+
typescriptOptions: { emitDecoratorMetadata: true },
29+
packages: {'app': {defaultExtension: 'ts'}}
30+
});
31+
System.import('app/main')
32+
.then(null, console.error.bind(console));
33+
</script>
34+
35+
</head>
36+
37+
<!-- 3. Display the application -->
38+
<body>
39+
<logger-app>Loading...</logger-app>
40+
</body>
41+
42+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Component} from 'angular2/core';
2+
import {Logger} from "angular2-logger/core";
3+
4+
@Component({
5+
selector: "hello-world",
6+
template: `
7+
<h1>Make sure your console is open.</h1>
8+
<h3>By default you should only see messages with a warning priority or higher.</h3>
9+
<button (click)="logMsgs()">Log Messages</button>
10+
`
11+
})
12+
export class HelloWorldComponent{
13+
14+
constructor(private _logger:Logger){}
15+
16+
logMsgs(){
17+
this._logger.error('This is a priority level 1 error message...');
18+
this._logger.warn('This is a priority level 2 warning message...');
19+
this._logger.info('This is a priority level 3 info message...');
20+
this._logger.debug('This is a priority level 4 debug message...');
21+
this._logger.log('This is a priority level 5 log message...');
22+
}
23+
24+
}

demos/hello-world/app/main.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {bootstrap} from 'angular2/platform/browser';
2+
import {HelloWorldComponent} from './hello-world.component';
3+
import {Logger} from "angular2-logger/core";
4+
5+
bootstrap(HelloWorldComponent,[
6+
Logger
7+
]);

0 commit comments

Comments
 (0)