Skip to content

Commit db8f841

Browse files
furkansenharputlumustafaiman
authored andcommitted
Refactor logging service
1 parent c3be92e commit db8f841

29 files changed

+287
-116
lines changed

code_samples/logging.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
var winston = require('winston');
1818
var Config = require('hazelcast-client').Config;
1919
var HazelcastClient = require('hazelcast-client').Client;
20+
var LogLevel = require('hazelcast-client').LogLevel;
2021

2122
if (process.argv.length != 3) {
2223
console.log('Run as node logging.js [logger]');
@@ -40,8 +41,28 @@ if (process.argv.length != 3) {
4041
'silly'
4142
],
4243

43-
log: function (level, className, message, furtherInfo) {
44-
this.logger.log(this.levels[level], className + ' ' + message);
44+
log: function (level, objectName, message, furtherInfo) {
45+
this.logger.log(this.levels[level], objectName + ': ' + message, furtherInfo);
46+
},
47+
48+
error: function (objectName, message, furtherInfo) {
49+
this.log(LogLevel.ERROR, objectName, message, furtherInfo);
50+
},
51+
52+
debug: function (objectName, message, furtherInfo) {
53+
this.log(LogLevel.DEBUG, objectName, message, furtherInfo);
54+
},
55+
56+
warn: function (objectName, message, furtherInfo) {
57+
this.log(LogLevel.WARN, objectName, message, furtherInfo);
58+
},
59+
60+
info: function (objectName, message, furtherInfo) {
61+
this.log(LogLevel.INFO, objectName, message, furtherInfo);
62+
},
63+
64+
trace: function (objectName, message, furtherInfo) {
65+
this.log(LogLevel.TRACE, objectName, message, furtherInfo);
4566
}
4667
};
4768
cfg.properties['hazelcast.logging'] = winstonAdapter;

src/HazelcastClient.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ export default class HazelcastClient {
9191
this.instanceName = 'hz.client_' + this.id;
9292
}
9393

94-
LoggingService.initialize(this.config.properties['hazelcast.logging'] as string);
95-
this.loggingService = LoggingService.getLoggingService();
94+
this.loggingService = new LoggingService(this.config.properties['hazelcast.logging'],
95+
this.config.properties['hazelcast.logging.level'] as number);
9696
this.invocationService = new InvocationService(this);
9797
this.listenerService = new ListenerService(this);
9898
this.serializationService = new SerializationServiceV1(this.config.serializationConfig);
@@ -398,10 +398,10 @@ export default class HazelcastClient {
398398
this.proxyManager.init();
399399
this.listenerService.start();
400400
this.statistics.start();
401-
this.loggingService.info('HazelcastClient', 'Client started');
401+
this.loggingService.getLogger().info('HazelcastClient', 'Client started');
402402
return this;
403403
}).catch((e) => {
404-
this.loggingService.error('HazelcastClient', 'Client failed to start', e);
404+
this.loggingService.getLogger().error('HazelcastClient', 'Client failed to start', e);
405405
throw e;
406406
});
407407
}
@@ -412,7 +412,7 @@ export default class HazelcastClient {
412412
const urlEndpoint = HazelcastCloudDiscovery.createUrlEndpoint(this.getConfig().properties,
413413
cloudConfig.discoveryToken);
414414
return new HazelcastCloudAddressTranslator(urlEndpoint, this.getConnectionTimeoutMillis(),
415-
this.loggingService);
415+
this.loggingService.getLogger());
416416
}
417417
return new DefaultAddressTranslator();
418418

@@ -436,7 +436,8 @@ export default class HazelcastClient {
436436
if (cloudConfig.enabled) {
437437
const discoveryToken = cloudConfig.discoveryToken;
438438
const urlEndpoint = HazelcastCloudDiscovery.createUrlEndpoint(this.getConfig().properties, discoveryToken);
439-
return new HazelcastCloudAddressProvider(urlEndpoint, this.getConnectionTimeoutMillis(), this.loggingService);
439+
return new HazelcastCloudAddressProvider(urlEndpoint, this.getConnectionTimeoutMillis(),
440+
this.loggingService.getLogger());
440441
}
441442
return null;
442443
}

src/HeartbeatService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {ClientPingCodec} from './codec/ClientPingCodec';
1818
import {ConnectionHeartbeatListener} from './core/ConnectionHeartbeatListener';
1919
import HazelcastClient from './HazelcastClient';
2020
import {ClientConnection} from './invocation/ClientConnection';
21-
import {LoggingService} from './logging/LoggingService';
21+
import {ILogger} from './logging/ILogger';
2222

2323
const PROPERTY_HEARTBEAT_INTERVAL: string = 'hazelcast.client.heartbeat.interval';
2424
const PROPERTY_HEARTBEAT_TIMEOUT: string = 'hazelcast.client.heartbeat.timeout';
@@ -31,14 +31,15 @@ export class Heartbeat {
3131
private heartbeatTimeout: number;
3232
private heartbeatInterval: number;
3333
private listeners: ConnectionHeartbeatListener[] = [];
34-
private logger = LoggingService.getLoggingService();
34+
private logger: ILogger;
3535

3636
// Actually it is a NodeJS.Timer. Another typing file that comes with a module we use causes TSD to see
3737
// return type of setTimeout as number. Because of this we defined timer property as `any` type.
3838
private timer: any;
3939

4040
constructor(client: HazelcastClient) {
4141
this.client = client;
42+
this.logger = this.client.getLoggingService().getLogger();
4243
this.heartbeatInterval = this.client.getConfig().properties[PROPERTY_HEARTBEAT_INTERVAL] as number;
4344
this.heartbeatTimeout = this.client.getConfig().properties[PROPERTY_HEARTBEAT_TIMEOUT] as number;
4445
}

src/ListenerService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import {ClientEventRegistration} from './invocation/ClientEventRegistration';
2626
import {Invocation} from './invocation/InvocationService';
2727
import {RegistrationKey} from './invocation/RegistrationKey';
2828
import {ListenerMessageCodec} from './ListenerMessageCodec';
29-
import {LoggingService} from './logging/LoggingService';
3029
import {copyObjectShallow, DeferredPromise} from './Util';
3130
import {UuidUtil} from './util/UuidUtil';
31+
import {ILogger} from './logging/ILogger';
3232

3333
export class ListenerService implements ConnectionHeartbeatListener {
3434
private client: HazelcastClient;
3535
private internalEventEmitter: EventEmitter;
36-
private logger = LoggingService.getLoggingService();
36+
private logger: ILogger;
3737
private isShutdown: boolean;
3838
private isSmartService: boolean;
3939

@@ -46,6 +46,7 @@ export class ListenerService implements ConnectionHeartbeatListener {
4646
constructor(client: HazelcastClient) {
4747
this.isShutdown = false;
4848
this.client = client;
49+
this.logger = this.client.getLoggingService().getLogger();
4950
this.isSmartService = this.client.getConfig().networkConfig.smartRouting;
5051
this.internalEventEmitter = new EventEmitter();
5152
this.internalEventEmitter.setMaxListeners(0);

src/PartitionService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import * as Promise from 'bluebird';
1818
import HazelcastClient from './HazelcastClient';
19-
import {LoggingService} from './logging/LoggingService';
19+
import {ILogger} from './logging/ILogger';
2020
import Address = require('./Address');
2121
import ClientMessage = require('./ClientMessage');
2222
import GetPartitionsCodec = require('./codec/GetPartitionsCodec');
@@ -30,10 +30,11 @@ export class PartitionService {
3030
private partitionCount: number;
3131
private partitionRefreshTask: any;
3232
private isShutdown: boolean;
33-
private logger = LoggingService.getLoggingService();
33+
private logger: ILogger;
3434

3535
constructor(client: HazelcastClient) {
3636
this.client = client;
37+
this.logger = client.getLoggingService().getLogger();
3738
this.isShutdown = false;
3839
}
3940

src/aggregation/AggregatorFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {LoggingService} from '../logging/LoggingService';
1817
import {IdentifiedDataSerializable, IdentifiedDataSerializableFactory} from '../serialization/Serializable';
1918
import {
2019
Aggregator,
@@ -31,6 +30,7 @@ import {
3130
MinAggregator,
3231
NumberAverageAggregator,
3332
} from './Aggregator';
33+
import {ILogger} from '../logging/ILogger';
3434

3535
export class AggregatorFactory implements IdentifiedDataSerializableFactory {
3636

@@ -56,7 +56,7 @@ export class AggregatorFactory implements IdentifiedDataSerializableFactory {
5656
static readonly MAX_BY = 17; // needs object to implement Java's Comparable interface
5757
static readonly MIN_BY = 18; // needs object to implement Java's Comparable interface
5858

59-
private logger = LoggingService.getLoggingService();
59+
private logger: ILogger;
6060
private idToConstructor: { [id: number]: Aggregator<any> } = {};
6161

6262
constructor() {

src/config/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {Properties} from './Properties';
2828
import {ReliableTopicConfig} from './ReliableTopicConfig';
2929
import {SerializationConfig} from './SerializationConfig';
3030
import {Statistics} from '../statistics/Statistics';
31+
import {LogLevel} from '..';
3132

3233
/**
3334
* Top level configuration object of Hazelcast client. Other configurations items are properties of this object.
@@ -45,6 +46,8 @@ export class ClientConfig {
4546
'hazelcast.invalidation.reconciliation.interval.seconds': 60,
4647
'hazelcast.invalidation.max.tolerated.miss.count': 10,
4748
'hazelcast.invalidation.min.reconciliation.interval.seconds': 30,
49+
'hazelcast.logging': 'default',
50+
'hazelcast.logging.level': LogLevel.INFO,
4851
};
4952

5053
/**

src/config/JsonConfigLocator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
import * as Promise from 'bluebird';
1818
import * as fs from 'fs';
1919
import * as Path from 'path';
20-
import {LoggingService} from '../logging/LoggingService';
20+
import {LoggingService, LogLevel} from '../logging/LoggingService';
2121
import {ConfigBuilder} from './ConfigBuilder';
2222
import {DeferredPromise} from '../Util';
23+
import {DefaultLogger} from '../logging/DefaultLogger';
2324

2425
export class JsonConfigLocator {
2526
static readonly ENV_VARIABLE_NAME = 'HAZELCAST_CLIENT_CONFIG';
2627
static readonly DEFAULT_FILE_NAME = 'hazelcast-client.json';
2728

2829
private buffer: Buffer;
2930
private configLocation: string;
30-
private logger = LoggingService.getLoggingService();
31+
private logger = new DefaultLogger(LogLevel.INFO);
3132

3233
load(): Promise<void> {
3334
return this.loadFromEnvironment().then((loaded: boolean) => {

src/config/Properties.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {ILogger} from '../logging/LoggingService';
17+
import {ILogger} from '../logging/ILogger';
1818

1919
export interface Properties {
2020
[prop: string]: string | number | boolean | ILogger;
2121
}
22+
23+
export type Property = string | number | boolean | ILogger;

src/discovery/HazelcastCloudAddressProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {LoggingService} from '../logging/LoggingService';
1817
import {HazelcastCloudDiscovery} from './HazelcastCloudDiscovery';
1918
import {AddressProvider} from '../connection/AddressProvider';
2019
import * as Promise from 'bluebird';
20+
import {ILogger} from '../logging/ILogger';
2121

2222
export class HazelcastCloudAddressProvider implements AddressProvider {
23-
private readonly loggingService: LoggingService;
23+
private readonly logger: ILogger;
2424
private readonly cloudDiscovery: HazelcastCloudDiscovery;
2525

26-
constructor(endpointUrl: string, connectionTimeoutMillis: number, loggingService: LoggingService) {
26+
constructor(endpointUrl: string, connectionTimeoutMillis: number, logger: ILogger) {
2727
this.cloudDiscovery = new HazelcastCloudDiscovery(endpointUrl, connectionTimeoutMillis);
28-
this.loggingService = loggingService;
28+
this.logger = logger;
2929
}
3030

3131
loadAddresses(): Promise<string[]> {
3232
return this.cloudDiscovery.discoverNodes().then((res) => {
3333
return Array.from(res.keys());
3434
}).catch((e) => {
35-
this.loggingService.warn('HazelcastCloudAddressProvider',
35+
this.logger.warn('HazelcastCloudAddressProvider',
3636
'Failed to load addresses from hazelcast.cloud : ' + e.message);
3737
return [];
3838
});

0 commit comments

Comments
 (0)