Skip to content

Commit

Permalink
feat(logger): custom tags in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nair-sumesh committed Mar 11, 2022
1 parent bfe7f6b commit 17eef1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
8 changes: 7 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ Example: `5-5-17-46-47-screenshot--some-id.png`

## Logger

For convenient console output, use `wdi5.getLogger()`. It supports the `syslog`-like levels `log`, `info`, `warn` and `error`:
For convenient console output, use `wdi5.getLogger('tag')`. It supports the `syslog`-like levels `log`, `info`, `warn` and `error`:

```javascript
const wdi5 = require("wdi5")
Expand All @@ -318,6 +318,12 @@ wdi5.getLogger().log("any", "number", "of", "log", "parts")
The log level is set by the either in `wdio.conf.js` via `wdi5.logLevel` or
by `wdi5.getLogger().setLoglevel(level = {string} "error"| "verbose" | "silent")`

The 'tag' is an optional parameter and when passed will display logs on the console log with a prefix as follows:

```cmd
[TAG] any number of log parts
```

## Navigation

In the test, you can navigate the UI5 webapp via `goTo(options)` in one of two ways:
Expand Down
19 changes: 12 additions & 7 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import { chalk as console } from "./coloredConsole"
import { wdi5LogLevel } from "../types/wdi5.types"

export class Logger {
private static instance: Logger | null = null
private constructor() {
private static instance
private constructor(sPrefix = "wdi5") {
this.prefix = `[${sPrefix}]`
// eliminate creating new instances
}

private prefix = "[wdi5]"
private prefix: string
private logLevel: wdi5LogLevel = "error"

static getInstance(): Logger {
if (Logger.instance === null) {
Logger.instance = new Logger()
static getInstance(sPrefix = "wdi5"): Logger {
if (!Logger.instance) {
Logger.instance = Object.assign({})
}
return Logger.instance

if (!Logger.instance[sPrefix]) {
Logger.instance[sPrefix] = new Logger(sPrefix)
}
return Logger.instance[sPrefix]
}

getLogLevel(): wdi5LogLevel {
Expand Down
8 changes: 6 additions & 2 deletions src/wdi5.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Logger as _Logger } from "./lib/Logger"
const Logger = _Logger.getInstance()
export class wdi5 {
static getLogger() {
return Logger
static getLogger(sPrefix = "wdi5") {
if (sPrefix === "wdi5") {
return Logger
} else {
return _Logger.getInstance(sPrefix)
}
}

static async goTo(param: any, oRoute) {
Expand Down

0 comments on commit 17eef1a

Please sign in to comment.