Skip to content

Commit dc7f866

Browse files
committed
docs: add documentation passing a logger to the client
1 parent ae85ef5 commit dc7f866

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,61 @@ client.locations.list().then(locations => {
3939

4040
```
4141

42+
## Logging
43+
44+
There is some logging done of requests and responses made to the API. The
45+
default logger does nothing but you can pass your own. Logging is done via a generic interface so
46+
you can use whatever logger you want in your application.
47+
48+
First, write an implementation of the `Logger` interface defined in
49+
[logger.ts](https://github.com/SmartThingsCommunity/smartthings-core-sdk/blob/master/src/logger.ts)
50+
which proxies to your logger. For example:
51+
52+
```javascript
53+
import { Logger as WinstonLogger } from 'winston'
54+
import { Logger } from '@smartthings/core-sdk'
55+
56+
57+
export class WinstonLoggerProxy implements Logger {
58+
proxy: WinstonLogger
59+
level: string
60+
61+
constructor(winstonLogger) {
62+
this.level = proxy.level
63+
}
64+
65+
trace(message: any, ...args: any[]): void {
66+
// Winston doesn't have a "trace" level but it has a "silly" level in the same place.
67+
proxy.silly(message, args)
68+
}
69+
70+
debug(message: any, ...args: any[]): void {
71+
proxy.debug(message, args)
72+
}
73+
74+
info(message: any, ...args: any[]): void {
75+
proxy.info(message, args)
76+
}
77+
78+
...
79+
80+
isTraceEnabled(): boolean {
81+
return proxy.isSillyEnabled()
82+
}
83+
84+
...
85+
}
86+
```
87+
88+
Then, when you create your `SmartThingsClient`, pass this in via the `config` parameter.
89+
90+
```javascript
91+
const config = {
92+
logger: new WinstonLoggerProxy(myWinstonLoggerInstance)
93+
}
94+
const client = new SmartThingsClient(new BearerTokenAuthenticator('{YOUR-PAT-TOKEN}'), config)
95+
```
96+
4297
## Reference
4398

4499
### Authenticators
@@ -94,4 +149,3 @@ and retry the original request.
94149

95150
* subscriptions - Operations for subscribing to events, for use in SmartApps and
96151
API Access apps. Link to code interface [here](https://github.com/SmartThingsCommunity/smartthings-core-sdk/blob/master/src/endpoint/subscriptions.ts#L213), link to wiki page description [here](https://github.com/SmartThingsCommunity/smartthings-core-sdk/wiki/Subscriptions)
97-

0 commit comments

Comments
 (0)