Skip to content

Commit 9509b74

Browse files
docs: add information for logger in Public API
1 parent 8b65272 commit 9509b74

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

PublicAPI.md

+159
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ const tns = require("nativescript");
7272
* [getPlaygroundAppQrCode](#getplaygroundappqrcode)
7373
* [cleanupService](#cleanupservice)
7474
* [setCleanupLogFile](#setcleanuplogfile)
75+
* [initializeService](#initializeService)
76+
* [initialize](#initialize)
77+
* [logger](#logger)
78+
* [initialize](#initialize)
79+
* [getLevel](#getlevel)
80+
* [appenders](#appenders)
81+
* [emit-appender](#emit-appender)
82+
* [cli-appender](#cli-appender)
83+
* [custom layouts](#custom-layouts)
84+
7585

7686
## Module projectService
7787

@@ -1514,6 +1524,155 @@ const tns = require("nativescript");
15141524
tns.cleanupService.setCleanupLogFile("/Users/username/cleanup-logs.txt");
15151525
```
15161526
1527+
## initializeService
1528+
The `initializeService` is used to initialize CLI's configuration and the beginning and print all warnings related to current environment.
1529+
1530+
### initialize
1531+
This method inits CLI's logger and prints all system warnings.
1532+
1533+
* Definition
1534+
```TypeScript
1535+
initialize(initOpts?: { loggerOptions?: ILoggerOptions }): Promise<void>
1536+
```
1537+
1538+
> NOTE: For more information about loggerOptions, you can check `logger`.
1539+
1540+
* Usage
1541+
```JavaScript
1542+
const tns = 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.
1562+
1563+
* Definition
1564+
```TypeScript
1565+
interface IAppenderOptions extends IDictionary<any> {
1566+
type: string;
1567+
layout?: Layout;
1568+
}
1569+
1570+
interface ILoggerOptions {
1571+
level?: LoggerLevel;
1572+
appenderOptions?: IAppenderOptions;
1573+
}
1574+
1575+
initialize(opts?: ILoggerOptions): void;
1576+
```
1577+
1578+
* Usage
1579+
* Initialize with default settings:
1580+
```JavaScript
1581+
tns.logger.initialize();
1582+
```
1583+
* Initialize with DEBUG log level:
1584+
```JavaScript
1585+
tns.logger.initialize({ level: tns.constants.LoggerLevel.DEBUG });
1586+
```
1587+
* Initialize with different appender, for example [fileSync](https://log4js-node.github.io/log4js-node/fileSync.html) appender:
1588+
```JavaScript
1589+
tns.logger.initialize({ appenderOptions: { type: "fileSync" } });
1590+
```
1591+
* Initialize with different layout, for example [Pattern](https://log4js-node.github.io/log4js-node/layouts.html#pattern) layout:
1592+
```JavaScript
1593+
tns.logger.initialize({ appenderOptions: { layout: { type: "pattern" } } });
1594+
```
1595+
* Initialize with custom appender, layout and level:
1596+
```JavaScript
1597+
tns.logger.initialize({ appenderOptions: { type: "fileSync", layout: { type: "pattern" } }, level: tns.constants.LoggerLevel.DEBUG });
1598+
```
1599+
1600+
### getLevel
1601+
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.
1618+
1619+
* Usage:
1620+
```JavaScript
1621+
const tns = require("nativescript");
1622+
const { EventEmitter } = require("events");
1623+
const { EMIT_APPENDER_EVENT_NAME, LoggerAppenders } = tns.constants;
1624+
const emitter = new EventEmitter();
1625+
// IMPORTANT: Add the event handler before calling logger's initialize method.
1626+
// This is required as log4js makes a copy of the appenderOptions, where the emitter is passed
1627+
// NOTE: In case you want to debug the event handler, place `debugger` in it.
1628+
emitter.on(EMIT_APPENDER_EVENT_NAME, (logData) => {
1629+
if (logData.loggingEvent.level.levelStr === LoggerLevel.WARN) {
1630+
console.log(`WARNING: ${logData.formattedMessage}`);
1631+
}
1632+
});
1633+
1634+
const logger = tns.logger;
1635+
logger.initialize({
1636+
appenderOptions: {
1637+
type: LoggerAppenders.emitAppender,
1638+
emitter
1639+
}
1640+
});
1641+
```
1642+
1643+
> 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.
1647+
1648+
* Usage
1649+
```JavaScript
1650+
const tns = require("nativescript");
1651+
const { EventEmitter } = require("events");
1652+
const { EMIT_APPENDER_EVENT_NAME, LoggerAppenders } = tns.constants;
1653+
1654+
const logger = tns.logger;
1655+
logger.initialize({
1656+
appenderOptions: {
1657+
type: LoggerAppenders.cliAppender,
1658+
}
1659+
});
1660+
```
1661+
1662+
### custom layouts
1663+
You can define your own layout function in the following way:
1664+
```JavaScript
1665+
const log4js = require("nativescript/node_modules/log4js");
1666+
const util = require("util");
1667+
log4js.addLayout("myCustomLayout", (config) => {
1668+
return (loggingEvent) => {
1669+
return util.format.apply(null, loggingEvent.data);
1670+
}
1671+
});
1672+
1673+
tns.logger.initialize({ appenderOptions: { layout: { type: "myCustomLayout" } } });
1674+
```
1675+
15171676
## How to add a new method to Public API
15181677
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.
15191678
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

Comments
 (0)