-
Notifications
You must be signed in to change notification settings - Fork 5
Small refactor #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small refactor #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
trim_trailing_whitespace = true | ||
indent_size = 2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
parser: "@typescript-eslint/parser" | ||
env: | ||
browser: true | ||
es6: true # Map, etc. | ||
jest: true | ||
node: true | ||
|
||
parserOptions: | ||
ecmaVersion: 2018 | ||
sourceType: module | ||
|
||
extends: | ||
- eslint:recommended | ||
- plugin:@typescript-eslint/recommended | ||
- plugin:import/recommended | ||
- plugin:import/typescript | ||
- plugin:prettier/recommended | ||
# Prettier should always be last | ||
# Removes eslint rules that conflict with prettier. | ||
- prettier | ||
|
||
rules: | ||
# For overloads. | ||
no-dupe-class-members: off | ||
eqeqeq: error | ||
import/order: | ||
[error, { alphabetize: { order: "asc" }, groups: [["builtin", "external", "internal"], "parent", "sibling"] }] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: ci | ||
|
||
on: | ||
push: | ||
branches: "**" | ||
tags-ignore: "v*" | ||
pull_request: | ||
|
||
jobs: | ||
prebuild: | ||
name: Pre-build checks | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v2 | ||
|
||
- name: Install Node.js v12 | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: "12" | ||
|
||
- name: Fetch dependencies from cache | ||
id: cache-yarn | ||
uses: actions/cache@v2 | ||
with: | ||
path: "**/node_modules" | ||
key: yarn-build-${{ hashFiles('**/yarn.lock') }} | ||
|
||
- name: Install dependencies | ||
if: steps.cache-yarn.outputs.cache-hit != 'true' | ||
run: yarn --frozen-lockfile | ||
|
||
- name: Run yarn lint | ||
run: yarn lint | ||
|
||
- name: Run yarn test | ||
run: yarn test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
printWidth: 95 | ||
semi: false | ||
trailingComma: all | ||
arrowParens: always |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
extends: | ||
- stylelint-config-recommended |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
{ | ||
"name": "@coder/logger", | ||
"description": "Beautiful logging inspired by https://github.com/uber-go/zap.", | ||
"scripts": { | ||
"prepare": "tsc", | ||
"test": "jest" | ||
}, | ||
"version": "1.1.16", | ||
"main": "out/index.js", | ||
"types": "out/index.d.ts", | ||
"author": "Coder", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"@google-cloud/logging": "^4.5.2" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.16", | ||
"@types/node": "^12.6.8", | ||
"jest": "^24.8.0", | ||
"ts-jest": "^24.0.2", | ||
"tslint": "^5.18.0", | ||
"typescript": "^3.5.3" | ||
}, | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"js", | ||
"ts" | ||
], | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest" | ||
}, | ||
"testRegex": ".*\\.test\\.ts" | ||
} | ||
"name": "@coder/logger", | ||
"description": "Beautiful logging inspired by https://github.com/uber-go/zap.", | ||
"scripts": { | ||
"lint": "eslint src/**", | ||
"prepare": "tsc", | ||
"test": "mocha --require ts-node/register ./test/*.test.ts" | ||
jsjoeio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"version": "2.0.0", | ||
"main": "out/index.js", | ||
"types": "out/index.d.ts", | ||
"author": "Coder", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"@google-cloud/logging": "^9.2.1" | ||
}, | ||
"devDependencies": { | ||
"@google-cloud/logging": "^9.2.1", | ||
"@types/mocha": "^8.2.2", | ||
"@types/node": "^12.6.8", | ||
"@typescript-eslint/eslint-plugin": "^4.22.0", | ||
"@typescript-eslint/parser": "^4.22.0", | ||
"eslint": "^7.25.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"mocha": "^8.3.2", | ||
"prettier": "^2.2.1", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^3.5.3" | ||
}, | ||
"dependencies": {} | ||
jsjoeio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { field, logger } from "./logger"; | ||
import { createStackdriverExtender } from "./extender"; | ||
import { createStackdriverExtender } from "./extender" | ||
import { field, logger } from "./logger" | ||
|
||
describe("Extender", () => { | ||
it("should add stackdriver extender", () => { | ||
logger.extend(createStackdriverExtender("coder-dev-1", "logging-package-tests")); | ||
}); | ||
it("should add stackdriver extender", () => { | ||
logger.extend(createStackdriverExtender("coder-dev-1", "logging-package-tests")) | ||
}) | ||
|
||
it("should log", async () => { | ||
logger.debug("Bananas!", field("frog", { hi: "wow" })); | ||
}); | ||
}); | ||
it("should log", async () => { | ||
logger.debug("Bananas!", field("frog", { hi: "wow" })) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,49 @@ | ||
// @ts-ignore | ||
import * as gcl from "@google-cloud/logging"; | ||
import { Extender, logger, field } from "./logger"; | ||
import * as gcl from "@google-cloud/logging" | ||
import { Extender, field, Level, logger } from "./logger" | ||
|
||
export const createStackdriverExtender = (projectId: string, logId: string): Extender => { | ||
enum GcpLogSeverity { | ||
DEFAULT = 0, | ||
DEBUG = 100, | ||
INFO = 200, | ||
NOTICE = 300, | ||
WARNING = 400, | ||
ERROR = 500, | ||
CRITICAL = 600, | ||
ALERT = 700, | ||
EMERGENCY = 800, | ||
} | ||
const logging = new gcl.Logging({ | ||
autoRetry: true, | ||
projectId, | ||
}) | ||
|
||
const logging = new gcl.Logging({ | ||
autoRetry: true, | ||
projectId, | ||
}); | ||
const log = logging.log(logId) | ||
const convertSeverity = (severity: Level): gcl.protos.google.logging.type.LogSeverity => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?: why do we do this? Or what is this rather? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our logger uses 0-4 to represent trace, debug, info, warn, and error, but Google Cloud uses something like 100, 200, 400, 500 (and has no trace level). So we need to convert the level to the right number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should also note logging to Google Cloud is optional. |
||
switch (severity) { | ||
case Level.Trace: | ||
case Level.Debug: | ||
return gcl.protos.google.logging.type.LogSeverity.DEBUG | ||
case Level.Info: | ||
return gcl.protos.google.logging.type.LogSeverity.INFO | ||
case Level.Warn: | ||
return gcl.protos.google.logging.type.LogSeverity.WARNING | ||
case Level.Error: | ||
return gcl.protos.google.logging.type.LogSeverity.ERROR | ||
} | ||
} | ||
|
||
const log = logging.log(logId); | ||
const convertSeverity = (severity: "trace" | "info" | "warn" | "debug" | "error"): GcpLogSeverity => { | ||
switch (severity) { | ||
case "trace": | ||
case "debug": | ||
return GcpLogSeverity.DEBUG; | ||
case "info": | ||
return GcpLogSeverity.INFO; | ||
case "error": | ||
return GcpLogSeverity.ERROR; | ||
case "warn": | ||
return GcpLogSeverity.WARNING; | ||
} | ||
}; | ||
return (options): void => { | ||
const severity = convertSeverity(options.level) | ||
const metadata: { [id: string]: string } = {} | ||
if (options.fields) { | ||
options.fields.forEach((f) => { | ||
if (!f) { | ||
return | ||
} | ||
metadata[f.identifier] = f.value | ||
}) | ||
} | ||
|
||
return (options): void => { | ||
const severity = convertSeverity(options.type); | ||
// tslint:disable-next-line:no-any | ||
const metadata = {} as any; | ||
if (options.fields) { | ||
options.fields.forEach((f) => { | ||
if (!f) { | ||
return; | ||
} | ||
metadata[f.identifier] = f.value; | ||
}); | ||
} | ||
const entry = log.entry( | ||
{ severity }, | ||
{ | ||
...metadata, | ||
message: options.message, | ||
}, | ||
) | ||
|
||
const entry = log.entry({ | ||
// tslint:disable-next-line:no-any | ||
severity: severity as any, | ||
}, { | ||
...metadata, | ||
message: options.message, | ||
}); | ||
|
||
log.write(entry).catch((ex: Error) => { | ||
logger.named("GCP").error("Failed to log", field("error", ex)); | ||
}); | ||
}; | ||
|
||
}; | ||
log.write(entry).catch((ex: Error) => { | ||
logger.named("GCP").error("Failed to log", field("error", ex)) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from "./logger"; | ||
export * from "./logger" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { field, logger, BrowserFormatter, Time } from "./logger"; | ||
import { field, logger, BrowserFormatter, Time } from "./logger" | ||
|
||
describe("Logger", () => { | ||
it("should use server formatter", () => { | ||
logger.info("test", field("key", "value"), field("time", new Time(100, Date.now()))); | ||
logger.named("name").debug("test name"); | ||
logger.named("another name").warn("another test name"); | ||
}); | ||
it("should use server formatter", () => { | ||
logger.info("test", field("key", "value"), field("time", new Time(100, Date.now()))) | ||
logger.named("name").debug("test name") | ||
logger.named("another name").warn("another test name") | ||
}) | ||
|
||
it("should use browser formatter", () => { | ||
logger.formatter = new BrowserFormatter(); | ||
logger.info("test", field("key", "value"), field("time", new Time(100, Date.now()))); | ||
logger.named("name").debug("test name"); | ||
logger.named("another name").warn("another test name"); | ||
}); | ||
}); | ||
it("should use browser formatter", () => { | ||
logger.formatter = new BrowserFormatter() | ||
logger.info("test", field("key", "value"), field("time", new Time(100, Date.now()))) | ||
logger.named("name").debug("test name") | ||
logger.named("another name").warn("another test name") | ||
}) | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.