From 6a4a674c81a40ddd41804373f30181799a43743d Mon Sep 17 00:00:00 2001 From: Hedgehog Date: Tue, 26 Apr 2016 09:45:45 +0200 Subject: [PATCH] added logging documentation from Strapi v1 --- mkdocs.yml | 1 + website/documentation/architecture/logging.md | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 website/documentation/architecture/logging.md diff --git a/mkdocs.yml b/mkdocs.yml index d5bdd8996d..83da0bf102 100755 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -40,6 +40,7 @@ pages: - Response: ./documentation/architecture/response.md - Databases: ./documentation/architecture/databases.md - Views: ./documentation/architecture/views.md + - Logging: ./documentation/architecture/logging.md - Concepts: - Authentication: ./documentation/concepts/authentication.md - GraphQL: ./documentation/concepts/graphql.md diff --git a/website/documentation/architecture/logging.md b/website/documentation/architecture/logging.md new file mode 100644 index 0000000000..8519abfa4e --- /dev/null +++ b/website/documentation/architecture/logging.md @@ -0,0 +1,95 @@ +# Logging + +Strapi comes with a simple and useful built-in logger. +Its usage is purposely very similar to `console.log()`, but with a handful of +extra features; namely support for multiple log levels with colorized, +prefixed console output. + +The logger is accessible through the `strapi` object directly with `strapi.log`. + +You can work with this logger in the same way that you work with the default logger: + +```js +strapi.log.info('Logs work!'); +``` + +## Logging with Metadata + +In addition to logging string messages, the logger will also optionally log additional +JSON metadata objects. Adding metadata is simple: + +```js +strapi.log.info('Test log message', { + anything: 'This is metadata' +}); +``` + +## String interpolation + +The log method provides the same string interpolation methods like `util.format`. + +This allows for the following log messages. + +```js +strapi.log.info('test message %s', 'my string'); +// => info: test message my string +``` + +```js +strapi.log.info('test message %d', 123); +// => info: test message 123 +``` + +```js +strapi.log.info('test message %j', { + number: 123 +}, {}); +// => info: test message {"number":123} +// => meta = {} +``` + +```js +strapi.log.info('test message %s, %s', 'first', 'second', { + number: 123 +}); +// => info: test message first, second +// => meta = {number: 123} +``` + +```js +strapi.log.info('test message', 'first', 'second', { + number: 123 +}); +// => info: test message first second +// => meta = {number: 123} +``` + +```js +strapi.log.info('test message %s, %s', 'first', 'second', { + number: 123 +}, function() {}); +// => info: test message first, second +// => meta = {number: 123} +// => callback = function() {} +``` + +```js +strapi.log.info('test message', 'first', 'second', { + number: 123 +}, function() {}); +// => info: test message first second +// => meta = {number: 123} +// => callback = function() {} +``` + +## Logging levels + +Setting the level for your logging message can be accomplished by using +the level specified methods defined. + +```js +strapi.log.debug('This is a debug log'); +strapi.log.info('This is an info log'); +strapi.log.warn('This is a warning log'); +strapi.log.error('This is an error log '); +```