Skip to content
This repository was archived by the owner on Aug 23, 2021. It is now read-only.

Commit

Permalink
feat: add log.level command (#55)
Browse files Browse the repository at this point in the history
resolves #50
  • Loading branch information
Alan Shaw authored Apr 5, 2019
1 parent 89975d3 commit 2da3b5d
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 3 deletions.
39 changes: 38 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* mpool.rm
* mpool.show
* [id](#id)
* log.level
* [log.level](#loglevel)
* log.ls
* [log.tail](#logtail)
* paych.close
Expand Down Expand Up @@ -542,6 +542,43 @@ console.log({ id: id.toStrng(), addresses: addresses.map(a => a.toString()) })
*/
```

## `log.level`

> Set the logging level for a subsystem or all subsystems
### `log.level(level, [options])`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| level | `String` | Log level to set. Available levels: debug, info, warning, error, fatal |
| options | `Object` | Optional options |
| options.subsystem | `String` | Subsystem to set the log level for. See [`log.ls`](#logls) for available subsystems |
| options.signal | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | A signal that can be used to abort the request |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<String>` | Confirmation message |

#### Example

Set log level for all subsystems:

```js
const msg = await fc.log.level('debug')
console.log(msg) // "Changed log level of all subsystems to: debug"
```

Set log level for "ping" subsystem:

```js
const msg = await fc.log.level('debug', { subsystem: 'ping' })
console.log(msg) // "Changed log level of 'ping' to 'debug'"
```

## `log.tail`

> Tail the logs
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ $ filecoin config api.accessControlAllowOrigin '["http://example.com"]'
* mpool.rm
* mpool.show
* [id](API.md#id)
* log.level
* [log.level](API.md#loglevel)
* log.ls
* [log.tail](API.md#logtail)
* paych.close
Expand All @@ -107,7 +107,7 @@ $ filecoin config api.accessControlAllowOrigin '["http://example.com"]'
* [wallet.export](API.md#walletexport)
* wallet.import

Status: 23/57 **40%**
Status: 24/57 **42%**

## Contribute

Expand Down
19 changes: 19 additions & 0 deletions src/cmd/log/level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const toUri = require('multiaddr-to-uri')
const QueryString = require('querystring')
const { ok } = require('../../lib/fetch')

module.exports = (fetch, config) => {
return async (level, options) => {
options = options || {}

const qs = { arg: level }

if (options.subsystem) {
qs.subsystem = options.subsystem
}

const url = `${toUri(config.apiAddr)}/api/log/level?${QueryString.stringify(qs)}`
const res = await ok(fetch(url, { signal: options.signal }))
return res.json()
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = (fetch, config) => {
},
id: require('./cmd/id')(fetch, config),
log: {
level: require('./cmd/log/level')(fetch, config),
tail: require('./cmd/log/tail')(fetch, config)
},
ping: require('./cmd/ping')(fetch, config),
Expand Down
43 changes: 43 additions & 0 deletions test/e2e/log/level.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const test = require('ava')
const CID = require('cids')
const Filecoin = require('../helpers/filecoin')

test('should set the log level for all subsystems', async t => {
const fc = Filecoin()

let res
res = await fc.log.level('error')
t.is(res, 'Changed log level of all subsystems to: error')

res = await fc.log.level('warning')
t.is(res, 'Changed log level of all subsystems to: warning')

res = await fc.log.level('info')
t.is(res, 'Changed log level of all subsystems to: info')
})

test('should set the log level for a specific subsystem', async t => {
const fc = Filecoin()

let res
res = await fc.log.level('error', { subsystem: 'ping' })
t.is(res, 'Changed log level of \'ping\' to \'error\'')

res = await fc.log.level('warning', { subsystem: 'ping' })
t.is(res, 'Changed log level of \'ping\' to \'warning\'')

res = await fc.log.level('info', { subsystem: 'ping' })
t.is(res, 'Changed log level of \'ping\' to \'info\'')
})

test('should error for unknown log level', async t => {
const fc = Filecoin()
const err = await t.throwsAsync(fc.log.level('foo'))
t.is(err.message, 'unknown log level: foo. Available levels: debug, info, warning, error, fatal, panic')
})

test('should error for unknown subsystem', async t => {
const fc = Filecoin()
const err = await t.throwsAsync(fc.log.level('info', { subsystem: 'foo' }))
t.is(err.message, 'Error: No such logger')
})
57 changes: 57 additions & 0 deletions test/unit/cmd/log/level.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const test = require('ava')
const Filecoin = require('../../../../src')

test('should set the log level for all subsystems', async t => {
const level = 'error'
const expectedMessage = `Changed log level of all subsystems to: ${level}`
const fetch = url => {
url = new URL(url)
t.is(url.searchParams.get('arg'), level)
return { ok: true, json: () => expectedMessage }
}
const fc = Filecoin(fetch)

const msg = await fc.log.level(level)
t.is(msg, expectedMessage)
})

test('should set the log level for a specific subsystem', async t => {
const level = 'error'
const subsystem = 'ping'
const expectedMessage = `Changed log level of \'${subsystem}\' to \'${level}\'`
const fetch = url => {
url = new URL(url)
t.is(url.searchParams.get('arg'), level)
t.is(url.searchParams.get('subsystem'), subsystem)
return { ok: true, json: () => expectedMessage }
}
const fc = Filecoin(fetch)

const msg = await fc.log.level(level, { subsystem })
t.is(msg, expectedMessage)
})

test('should error for unknown log level', async t => {
const level = 'foo'
const expectedMessage = `'unknown log level: ${level}. Available levels: debug, info, warning, error, fatal, panic'`
const fetch = url => {
return { ok: false, text: () => JSON.stringify({ Message: expectedMessage }) }
}
const fc = Filecoin(fetch)

const err = await t.throwsAsync(fc.log.level(level))
t.is(err.message, expectedMessage)
})

test('should error for unknown subsystem', async t => {
const level = 'error'
const subsystem = 'foo'
const expectedMessage = 'Error: No such logger'
const fetch = url => {
return { ok: false, text: () => JSON.stringify({ Message: expectedMessage }) }
}
const fc = Filecoin(fetch)

const err = await t.throwsAsync(fc.log.level(level, { subsystem }))
t.is(err.message, expectedMessage)
})

0 comments on commit 2da3b5d

Please sign in to comment.