|
| 1 | +# TypeScript Logging |
| 2 | + |
| 3 | +TypeScript logging can be used to add logging to your web or node project. There are two different flavors available to |
| 4 | +use. Please visit either of these below and pick the one you prefer. |
| 5 | + |
| 6 | +* The [typescript-logging-category-style](./category-style/README.MD) flavor |
| 7 | +* The [typescript-logging-log4ts-style](./log4ts-style/README.MD) flavor |
| 8 | + |
| 9 | +The following general sections are available: |
| 10 | + |
| 11 | +* [Getting started](#getting-started) |
| 12 | +* [Build](#build) |
| 13 | +* [Tests](#tests) |
| 14 | +* [Bugs](#bugs) |
| 15 | +* [Contributing](#contributing) |
| 16 | +* [Migration](#migration) |
| 17 | +* [Changelog](#changelog) |
| 18 | + |
| 19 | +## Getting started |
| 20 | + |
| 21 | +For all details and documentation please visit the links above. The following sections provide a quick start only for |
| 22 | +both flavors. |
| 23 | + |
| 24 | +### Category style flavor |
| 25 | + |
| 26 | +*To install the category-style flavor use the following npm command:* |
| 27 | + |
| 28 | +```shell |
| 29 | +npm install --save typescript-logging-category-style |
| 30 | +``` |
| 31 | + |
| 32 | +*Usage* |
| 33 | + |
| 34 | +The following section configures a provider and exposes a getLogger function for other modules to use. The getLogger in |
| 35 | +this example is used to create root categories. |
| 36 | + |
| 37 | +```typescript |
| 38 | +/*--- LogConfig.ts ---*/ |
| 39 | +import {CategoryProvider, Category} from "typescript-logging-category-style"; |
| 40 | + |
| 41 | +const provider = CategoryProvider.createProvider("ExampleProvider"); |
| 42 | + |
| 43 | +export function getLogger(name: string): Category { |
| 44 | + return provider.getCategory(name); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +---- |
| 49 | + |
| 50 | +```typescript |
| 51 | +/*--- Person.ts ---*/ |
| 52 | +import {getLogger} from "./LogConfig"; |
| 53 | + |
| 54 | +/* Root categories can and probably will be defined elsewhere, this is just an example */ |
| 55 | +const logModel = getLogger("model"); |
| 56 | + |
| 57 | +/* Create child categories based on a parent category, effectively allowing you to create a tree of loggers when needed */ |
| 58 | +const logPerson = logModel.getChildCategory("Person"); |
| 59 | + |
| 60 | +function example(value: string) { |
| 61 | + logPerson.debug(() => `Example function called with value ${value}`); |
| 62 | + try { |
| 63 | + // Awesome code here... |
| 64 | + logPerson.getChildCategory("example()").debug(() => "Child category again"); |
| 65 | + } |
| 66 | + catch (e) { |
| 67 | + logPerson.error(() => "Awesome code failed unexpectedly", e); |
| 68 | + } |
| 69 | + finally { |
| 70 | + logPerson.debug(() => "Example function completed"); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Log4ts flavor |
| 76 | + |
| 77 | +*To install the log4ts-style flavor use the following npm command:* |
| 78 | + |
| 79 | +```shell |
| 80 | +npm install --save typescript-logging-log4ts-style |
| 81 | +``` |
| 82 | + |
| 83 | +*Usage* |
| 84 | + |
| 85 | +The following section configures a provider and exposes a getLogger function for other modules to use. |
| 86 | + |
| 87 | +```typescript |
| 88 | +/*--- LogConfig.ts ---*/ |
| 89 | +import {Log4TSProvider, Logger, LogLevel} from "typescript-logging-log4ts-style"; |
| 90 | + |
| 91 | +const provider = Log4TSProvider.createProvider("ExampleProvider", { |
| 92 | + /* Specify the various group expressions to match against */ |
| 93 | + groups: [{ |
| 94 | + expression: new RegExp("model.+"), |
| 95 | + level: LogLevel.Debug, /* This group will log on debug instead */ |
| 96 | + }, { |
| 97 | + expression: new RegExp("service.+"), |
| 98 | + }], |
| 99 | +}); |
| 100 | + |
| 101 | +export function getLogger(name: string): Logger { |
| 102 | + return provider.getLogger(name); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +---- |
| 107 | + |
| 108 | +```typescript |
| 109 | +/*--- Person.ts ---*/ |
| 110 | +import {getLogger} from "./LogConfig"; |
| 111 | + |
| 112 | +const log = getLogger("model.Person") |
| 113 | + |
| 114 | +function example(value: string) { |
| 115 | + log.debug(() => `Example function called with value ${value}`); |
| 116 | + try { |
| 117 | + // Awesome code here... |
| 118 | + } |
| 119 | + catch (e) { |
| 120 | + log.error(() => "Awesome code failed unexpectedly", e); |
| 121 | + } |
| 122 | + finally { |
| 123 | + log.debug(() => "Example function completed"); |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## Build |
| 129 | + |
| 130 | +To locally build the logging flavors. |
| 131 | + |
| 132 | +```shell |
| 133 | +npm run ci # If not installed yet |
| 134 | +npm run build --workspaces # Builds all flavors, going into respective directory doing just 'npm run build' works too. |
| 135 | +``` |
| 136 | + |
| 137 | +## Tests |
| 138 | + |
| 139 | +To locally run the tests: |
| 140 | + |
| 141 | +```shell |
| 142 | +npm run ci # If not installed yet |
| 143 | +npm run test --workspaces # Tests all flavors, going into respective directory doing just 'npm run test' works too. |
| 144 | +``` |
| 145 | + |
| 146 | +## Integration tests |
| 147 | + |
| 148 | +If you're on linux or mac-os, it's easiest to run initialize.sh first. Otherwise, skip that and run npm run ci manually |
| 149 | +as shown below. |
| 150 | + |
| 151 | +```shell |
| 152 | +# Linux/MacOS only - Cleans everything and re-installs packages, including those for the integration projects. |
| 153 | +./initialize.sh |
| 154 | + |
| 155 | +# Use when ./initialize.sh cannot be used |
| 156 | +npm run ci # Run inside respective test-integration project, e.g. tests-integration/rollup |
| 157 | +npm run build # Run inside respective test-integration project. Builds test webapp and runs cypress tests. |
| 158 | +``` |
| 159 | + |
| 160 | +## Bugs |
| 161 | + |
| 162 | +If you encounter a bug please log it in the issue tracker of this repository. |
| 163 | + |
| 164 | +## Contributing |
| 165 | + |
| 166 | +Feel free to contribute or come up with great ideas, please use the issue tracker for that. |
| 167 | + |
| 168 | +If you add/change new functionality and want it merged in a later release, please open a pull request. Also add tests |
| 169 | +for it (see various "test" directories). |
| 170 | + |
| 171 | +Please keep in mind that things may not fit the library and in such case will be rejected, so if you are unsure please |
| 172 | +ask first before wasting your valuable time. |
| 173 | + |
| 174 | +# Migration |
| 175 | + |
| 176 | +Please check the [migration guide](documentation/migration.md) if you are on an old version and wish to use the latest |
| 177 | +version available. |
| 178 | + |
| 179 | +## Changelog |
| 180 | + |
| 181 | +Please check the [changelog](documentation/change_log.md) |
0 commit comments