You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> NOTE: For more information about loggerOptions, you can check `logger`.
1539
+
1540
+
* Usage
1541
+
```JavaScript
1542
+
consttns=require("nativescript");
1543
+
tns.initializeService.initialize();
1544
+
```
1545
+
1546
+
## logger
1547
+
1548
+
`logger` module is used to show any kind of information to the user. The `logger` uses `log4js` internally, which allows setting different levels for the messages.
1549
+
The levels are available in `tns.constants.LoggerLevel` enum. Only messages from the current log level (or higher) are shown to the user, i.e. in case the log level is set to `INFO`, `DEBUG` and `TRACE` messages will not be shown to the user, but `WARN` and `ERROR` messages will be shown. </br>
1550
+
`logger` module can be configured how to show the messages by using different appenders and layouts. </br>
1551
+
* `appenders` are responsible for output of log events. They may write events to files, send emails, store them in a database, or anything. Most appenders use layouts to serialise the events to strings for output.
1552
+
* `layout` is a function for converting a LogEvent into a string representation.
1553
+
1554
+
`log4js` has predefined appenders and layouts that can be used. In case you do not pass any options to logger's initialization, CLI will default to [console appender](https://log4js-node.github.io/log4js-node/console.html) with [messagePassThrough layout](https://log4js-node.github.io/log4js-node/layouts.html#message-pass-through) with `INFO` log level.</br>
1555
+
You can override only the properties you want, i.e. only the log level, the layout or the appender. </br>
1556
+
`nativescript` itself has additional appenders that you can use. More information about them can be found below. You can get a full list of the available appenders by checking the `tns.constants.LoggerAppenders` object. </br>
1557
+
1558
+
> NOTE: When CLI is used as a command-line tool, it uses a custom appender and layout in order to write coloured messages to stdout or stderr.
1559
+
1560
+
### initialize
1561
+
The `initialize` method initializes the log4js settings - level, appender and layout. Once called, the settings cannot be changed anymore for the current process.
This method returns information for the current log level.
1602
+
1603
+
* Definition
1604
+
```TypeScript
1605
+
getLevel(): string;
1606
+
```
1607
+
1608
+
* Usage
1609
+
```JavaScript
1610
+
console.log(`Current log level is: ${tns.logger.getLevel()}`);
1611
+
```
1612
+
1613
+
### appenders
1614
+
The `appenders` are log4js concept. `appenders` are responsible for output of log events. You can use all predefined [log4js appenders](https://log4js-node.github.io/log4js-node/appenders.html) and also several predefined CLI appenders
1615
+
1616
+
#### emit-appender
1617
+
The `emit-appender` is used to emit the log events through a passed emitter instead of writing the messages. Whenever a message should be shown, the `emit-appender` emits `logData` event with an object containing the `loggingEvent` and the message passed through the specified layout stored in `formattedMessage` property.
> NOTE: In several cases CLI passes additional configuration properties in the `context` of the `loggingEvent`. Full list is available in the `tns.constants.LoggerConfigData` object. These properties are used by CLI's layout and appender to change the way the message is printed on the terminal and if it should be on stderr or stdout.
1644
+
1645
+
#### cli-appender
1646
+
`cli-appender` prints messages to stdout or stderr based on the passed options for the message.
CLI is designed as command line tool and when it is used as a library, it does not give you access to all of the methods. This is mainly implementation detail. Most of the CLI's code is created to work in command line, not as a library, so before adding method to public API, most probably it will require some modification.
1519
1678
For example the `$options` injected module contains information about all `--` options passed on the terminal. When the CLI is used as a library, the options are not populated. Before adding method to public API, make sure its implementation does not rely on `$options`.
0 commit comments