Skip to content

Commit

Permalink
Fix: logger & error handling minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Dec 19, 2018
1 parent 9e6a385 commit 027bc50
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/spec/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Method | Function
`formatPercent(value: any, digits?: string | DigitsOptions, defaultLocale?: string): string` | Formats a number as a percentage according to default locale
`formatCurrency(value: any, digits?: string | DigitsOptions, currencyDisplay?: string, defaultLocale?: string, currency?: string): string` | Formats a number as a currency according to default locale
`composeLocale(codes: ISOCode[]): string` |
`rollback(): void` | Rollbacks to previous default locale, currency & timezone
`rollback(): void` | Rollbacks to previous language, default locale, currency & timezone

---

Expand Down
31 changes: 7 additions & 24 deletions src/models/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { ReplaySubject } from "rxjs";
import { L10N_CONFIG, L10nConfigRef } from "./l10n-config";
import { LogLevel, Log, LOG_MESSAGES } from "./types";

/**
* The buffer is necessary to the logger because during the initialization of the decorators the services are not yet available.
*/
@Injectable() export class Logger {

private static level: LogLevel | null = null;
Expand All @@ -15,35 +12,21 @@ import { LogLevel, Log, LOG_MESSAGES } from "./types";

public static log(name: string, message: string): void {
if (Logger.level == LogLevel.Off) return;

if (Logger.level) {
Logger.send({ name: name, message: message });
} else {
Logger.buffer.next({ name: name, message: message });
}
}

public static send(log: Log): void {
const message: string = `angular-l10n (${log.name}): ${LOG_MESSAGES[log.message]}`;
switch (Logger.level) {
case LogLevel.Error:
console.error(message);
break;
case LogLevel.Warn:
console.warn(message);
break;
default:
console.log(message);
}
Logger.buffer.next({ name: name, message: message });
}

constructor(@Inject(L10N_CONFIG) private configuration: L10nConfigRef) {
Logger.level = this.configuration.logger.level || LogLevel.Off;
if (Logger.level != LogLevel.Off) {
Logger.buffer.subscribe((log: Log) => {
Logger.send(log);
this.send(log);
});
}
}

private send(log: Log): void {
const message: string = `angular-l10n (${log.name}): ${LOG_MESSAGES[log.message]}`;
(console as any)[Logger.level!](message);
}

}
9 changes: 5 additions & 4 deletions src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ export enum ExtraCode {

export enum LogLevel {

Error,
Warn,
Log,
Off
Error = 'error',
Warn = 'warn',
Info = 'info',
Log = 'log',
Off = 'off'

}

Expand Down
6 changes: 3 additions & 3 deletions src/services/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import { takeUntilDestroyed } from '../models/take-until-destroyed';
*/
@Injectable() export class Localization extends Translation {

public defaultLocale: string;
public currency: string;
public timezone: string;
public defaultLocale: string = '';
public currency: string = '';
public timezone: string = '';

constructor(protected changeDetectorRef?: ChangeDetectorRef) {
super();
Expand Down
11 changes: 8 additions & 3 deletions src/services/locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export interface ILocaleService {
private currencyCode: string;
private timezone: string;

private rollbackLanguageCode: string;
private rollbackDefaultLocale: string;
private rollbackCurrencyCode: string;
private rollbackTimezone: string;
Expand Down Expand Up @@ -205,7 +206,7 @@ export interface ILocaleService {

public setCurrentLanguage(languageCode: string): void {
if (this.defaultLocale.languageCode != languageCode) {
this.rollbackDefaultLocale = this.defaultLocale.value;
this.rollbackLanguageCode = this.defaultLocale.languageCode;
this.defaultLocale.build(languageCode);
this.releaseLanguage();
}
Expand Down Expand Up @@ -327,9 +328,13 @@ export interface ILocaleService {
}

/**
* Rollbacks to previous default locale, currency & timezone.
* Rollbacks to previous language, default locale, currency & timezone.
*/
public rollback(): void {
if (this.rollbackLanguageCode && this.rollbackLanguageCode != this.defaultLocale.languageCode) {
this.defaultLocale.value = this.rollbackLanguageCode;
this.releaseLanguage();
}
if (this.rollbackDefaultLocale && this.rollbackDefaultLocale != this.defaultLocale.value) {
this.defaultLocale.value = this.rollbackDefaultLocale;
this.releaseDefaultLocale();
Expand Down Expand Up @@ -363,7 +368,7 @@ export interface ILocaleService {
}
this.storage.write("defaultLocale", this.defaultLocale.value);
}
this.rollbackDefaultLocale = this.defaultLocale.value;
this.rollbackLanguageCode = this.defaultLocale.languageCode;
this.sendLanguageEvents();
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/models/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
<p>{{ 1.22 | l10nDecimal:defaultLocale:'1-2.1' }}</p>
`
})
class LanguageComponent {
class MockComponent {

@Language() lang: string;
@DefaultLocale() defaultLocale: string
Expand All @@ -30,7 +30,7 @@ class LanguageComponent {
describe('Logger', () => {

let comp: any;
let fixture: ComponentFixture<LanguageComponent>;
let fixture: ComponentFixture<MockComponent>;

let l10nLoader: L10nLoader;

Expand All @@ -50,12 +50,12 @@ describe('Logger', () => {

beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [LanguageComponent],
declarations: [MockComponent],
imports: [
HttpClientTestingModule,
LocalizationModule.forRoot(l10nConfig)
],
}).createComponent(LanguageComponent);
}).createComponent(MockComponent);

comp = fixture.componentInstance;
});
Expand All @@ -69,11 +69,11 @@ describe('Logger', () => {
it('should log missing functions', (() => {
fixture.detectChanges();

expect(console.warn).toHaveBeenCalledWith("angular-l10n (LanguageComponent): Missing 'ngOnInit' method: required by AoT compilation");
expect(console.warn).toHaveBeenCalledWith("angular-l10n (MockComponent): Missing 'ngOnInit' method: required by AoT compilation");
expect(console.warn).toHaveBeenCalledWith("angular-l10n (TranslatePipe): Missing 'lang' parameter");

comp.ngOnDestroy();
expect(console.warn).toHaveBeenCalledWith("angular-l10n (LanguageComponent): Missing 'ngOnDestroy' method to cancel subscriptions: required by AoT compilation");
expect(console.warn).toHaveBeenCalledWith("angular-l10n (MockComponent): Missing 'ngOnDestroy' method to cancel subscriptions: required by AoT compilation");
}));

it('should log invalid formats', (() => {
Expand Down

0 comments on commit 027bc50

Please sign in to comment.