Skip to content

Commit

Permalink
Run yarn lint:save
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Sep 8, 2023
1 parent 2f039e5 commit 95a679e
Show file tree
Hide file tree
Showing 39 changed files with 402 additions and 271 deletions.
1 change: 1 addition & 0 deletions packages/browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Experience SQL-compatible structured log management based on ClickHouse. [Learn
[Getting started ⇗](https://betterstack.com/docs/logs/javascript/)

## Need help?

Please let us know at [hello@betterstack.com](mailto:hello@betterstack.com). We're happy to help!

---
Expand Down
17 changes: 12 additions & 5 deletions packages/browser/src/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Browser } from "./browser";
*/
function getRandomLog(message: string): Partial<ILogtailLog> {
return {
message
message,
};
}

Expand All @@ -26,7 +26,9 @@ describe("browser tests", () => {
// });

it("should echo log if logtail sends 20x status code", async done => {
nock("https://in.logtail.com").post('/').reply(201);
nock("https://in.logtail.com")
.post("/")
.reply(201);

const message: string = String(Math.random());
const expectedLog = getRandomLog(message);
Expand All @@ -38,9 +40,14 @@ describe("browser tests", () => {
});

it("should throw error if logtail sends non 200 status code", async done => {
nock("https://in.logtail.com").post('/').reply(401);

const browser = new Browser("invalid source token", { ignoreExceptions: false, throwExceptions: true });
nock("https://in.logtail.com")
.post("/")
.reply(401);

const browser = new Browser("invalid source token", {
ignoreExceptions: false,
throwExceptions: true,
});
const message: string = String(Math.random);
await expect(browser.log(message)).rejects.toThrow();

Expand Down
38 changes: 16 additions & 22 deletions packages/browser/src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'cross-fetch/polyfill';
import "cross-fetch/polyfill";

import { ILogtailLog, ILogtailOptions } from "@logtail/types";
import { Base } from "@logtail/core";
Expand All @@ -7,28 +7,22 @@ import { Base } from "@logtail/core";
// import { getUserAgent } from "./helpers";

export class Browser extends Base {
public constructor(
sourceToken: string,
options?: Partial<ILogtailOptions>
) {
public constructor(sourceToken: string, options?: Partial<ILogtailOptions>) {
super(sourceToken, options);

// Sync function
const sync = async (logs: ILogtailLog[]): Promise<ILogtailLog[]> => {
const res = await fetch(
this._options.endpoint,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this._sourceToken}`
// Awaiting: https://bugs.chromium.org/p/chromium/issues/detail?id=571722
// "User-Agent": getUserAgent()
},
body: JSON.stringify(logs),
keepalive: true,
}
);
const res = await fetch(this._options.endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this._sourceToken}`,
// Awaiting: https://bugs.chromium.org/p/chromium/issues/detail?id=571722
// "User-Agent": getUserAgent()
},
body: JSON.stringify(logs),
keepalive: true,
});

if (res.ok) {
return logs;
Expand All @@ -48,12 +42,12 @@ export class Browser extends Base {
}

private configureFlushOnPageLeave() {
if (typeof document === 'undefined') {
if (typeof document === "undefined") {
return;
}

document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") {
this.flush();
}
});
Expand Down
1 change: 0 additions & 1 deletion packages/browser/src/umd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import { Browser } from "./browser";

(window as any).Logtail = Browser;
16 changes: 8 additions & 8 deletions packages/browser/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const umdConfig: webpack.Configuration = {
output: {
libraryTarget: "umd",
path: path.join(__dirname, "dist", "umd"),
filename: "logtail.js"
filename: "logtail.js",
},
optimization: {
usedExports: true
usedExports: true,
},
module: {
rules: [
Expand All @@ -21,14 +21,14 @@ const umdConfig: webpack.Configuration = {
options: {
transpileOnly: true,
context: __dirname,
configFile: "tsconfig.umd.json"
}
}
]
configFile: "tsconfig.umd.json",
},
},
],
},
resolve: {
extensions: [".ts", ".js"]
}
extensions: [".ts", ".js"],
},
};

export default umdConfig;
1 change: 1 addition & 0 deletions packages/bunyan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Experience SQL-compatible structured log management based on ClickHouse. [Learn
[Getting started ⇗](https://betterstack.com/docs/logs/javascript/bunyan)

## Need help?

Please let us know at [hello@betterstack.com](mailto:hello@betterstack.com). We're happy to help!

---
Expand Down
14 changes: 7 additions & 7 deletions packages/bunyan/src/bunyan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function createLogger(logtail: Logtail): bunyan {
level: "debug", // <-- default to 'debug' and above
streams: [
{
stream: new LogtailStream(logtail)
}
]
stream: new LogtailStream(logtail),
},
],
});
}

Expand All @@ -37,7 +37,7 @@ function createLogger(logtail: Logtail): bunyan {
async function testLevel(
level: LogLevelString,
logLevel: LogLevel,
cb: Function
cb: Function,
) {
// Logtail fixtures
const logtail = new Logtail("test", { batchInterval: 1 });
Expand Down Expand Up @@ -92,12 +92,12 @@ describe("Bunyan tests", () => {
[30, LogLevel.Info, "info"],
[40, LogLevel.Warn, "warn"],
[50, LogLevel.Error, "error"],
[60, LogLevel.Fatal, "fatal"]
[60, LogLevel.Fatal, "fatal"],
];

const logtail = new Logtail("test", {
batchInterval: 1000,
batchSize: levels.length
batchSize: levels.length,
});

logtail.setSync(async logs => {
Expand Down Expand Up @@ -131,7 +131,7 @@ describe("Bunyan tests", () => {
const logtail = new Logtail("test");
logtail.setSync(async logs => {
const context = logs[0].context;
expect(context.runtime.file).toMatch("bunyan.test.ts")
expect(context.runtime.file).toMatch("bunyan.test.ts");
done();
return logs;
});
Expand Down
17 changes: 13 additions & 4 deletions packages/bunyan/src/bunyan.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Writable } from "stream";

import { Logtail } from "@logtail/node";
import {Context, LogLevel, StackContextHint} from "@logtail/types";
import { Context, LogLevel, StackContextHint } from "@logtail/types";

const stackContextHint = { fileName: "node_modules/bunyan", methodNames: [ "fatal", "error", "warn", "info", "debug", "trace" ] };
const stackContextHint = {
fileName: "node_modules/bunyan",
methodNames: ["fatal", "error", "warn", "info", "debug", "trace"],
};

import { getLogLevel } from "./helpers";

Expand Down Expand Up @@ -48,7 +51,8 @@ export class LogtailStream extends Writable {
// Get message
// NOTE: Bunyan passes empty 'msg' when msg is missing
const use_msg_field = log.msg !== undefined && log.msg.length > 0;
const msg = (use_msg_field ? log.msg : log.message) || "<no message provided>";
const msg =
(use_msg_field ? log.msg : log.message) || "<no message provided>";

// Prevent overriding 'message' with 'msg'
// Save 'message' as 'message_field' if we are using 'msg' as message
Expand All @@ -60,7 +64,12 @@ export class LogtailStream extends Writable {
const level = getLogLevel(log.level);

// Log to Logtail
void this._logtail.log(msg, level, meta, stackContextHint as StackContextHint);
void this._logtail.log(
msg,
level,
meta,
stackContextHint as StackContextHint,
);

next();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bunyan/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LogLevel, ILogLevel } from "@logtail/types";
export function getLogLevel(level: number | string): ILogLevel {
// Are we dealing with a string log level?
if (typeof level === "string") {
return level.toLowerCase()
return level.toLowerCase();
}
if (typeof level === "number") {
// Trace
Expand Down
7 changes: 2 additions & 5 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ import { ILogtailOptions, ILogtailLog } from "@logtail/types";

class CustomLogger extends Base {
// Constructor must take a Logtail.com source token, and (optional) options
public constructor(
sourceToken: string,
options?: Partial<ILogtailOptions>
) {
public constructor(sourceToken: string, options?: Partial<ILogtailOptions>) {
// Make sure you pass the source token to the parent constructor!
super(sourceToken, options);

Expand Down Expand Up @@ -80,7 +77,7 @@ All log levels return a Promise that will resolve once the log has been synced w

```typescript
// Will resolve when synced with Logtail.com (or reject if there's an error)
logtail.log("some log message").then((log) => {
logtail.log("some log message").then(log => {
// `log` is the transformed log, after going through middleware
});
```
Expand Down
8 changes: 4 additions & 4 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module.exports = {
coveragePathIgnorePatterns: ["/node_modules/", "/dist/"],
globals: {
"ts-jest": {
tsConfig: "tsconfig.json"
}
tsConfig: "tsconfig.json",
},
},
moduleDirectories: ["node_modules"],
moduleFileExtensions: ["ts", "tsx", "js"],
testEnvironment: "node",
testMatch: ["**/*.test.ts"],
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest"
}
"^.+\\.(ts|tsx)$": "ts-jest",
},
};
Loading

0 comments on commit 95a679e

Please sign in to comment.