README.md 2.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
# Log Schema Node

This is a Node module intended to make the
[nsd-commons/log-schema](https://gitlab.nsd.uib.no/nsd-commons/log-schema)
consumeable by Node applications. It exposes the
[JSON Schema](http://json-schema.org/) based log schema as a Javascript object,
and a corresponding Typescript definitions file.


## Installation

If you use npm:
```bash
# Add nexus.nsd.no npm repo if not already added:
npm set registry https://nexus.nsd.no/repository/npm-group/
npm install --save @nsd/log-schema-node
```

If you use yarn:
```bash
# Add nexus.nsd.no npm repo if not already added:
npm set registry https://nexus.nsd.no/repository/npm-group/
yarn add @nsd/log-schema-node
```

## Usage

Typescript example below. ES6 would look much the same.
```typescript
import {LogSchema, schema} from "@nsd/log-schema-node";
import * as JaySchema from "jayschema";

const js = new JaySchema();

const logEvent: LogSchema = {
    schemaVersion: "v3",
    "@timestamp": "1985-04-12T23:20:50.52Z",
    levelName: "INFO",
    message: "Ørliten melding",
    serviceName: "some-service",
    serviceVersion: "v1.0.0"
};

console.log(js.validate(logEvent, schema));
```

47
Example using the ExpressLogger:
Eirik Alvær's avatar
Eirik Alvær committed
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
```typescript
    import Express = require("express");
    import {ExpressLogger as Logger} from "@nsd/log-schema-node";

    const app = Express();

    app.use(Logger.middleware);

    app.get("/hello", (req, res) => {
        Logger.info({
            message: "Hello, Logger!",
            req
        });
        res.send("Hello, World!");
    });

    const port = 1337;
    const server = app.listen(port, () => {
        Logger.info({ message:  "Server started on port " + port});
    });

    server.on("error", (err) => {
        Logger.error({
            message: "Server has encountered an error.",
            error: {
                code: 100,
                type: "SERVER_ERROR",
                originalErrorObject: err
            }
        });
    });
```

82 83 84 85 86
By using the ExpressLogger middleware basic info about each request and response will be logged.

## Deploy

When comitting a tag to gitlab the npm module will be deployed as "@nsd/log-schema-node".