Comments (4)
Any progress on this @adikus?
from logtail-js.
Thanks @pimmee for the suggestions. Most of these make sense to me, I'll make a ticket in our internal system so that we can get back to this.
There's couple of things though:
-
I'm not sure if we'd want to support
unknown
types in the library, I would prefer we left that to the user to cast it toError
first (but we should defo supportError
). -
In your Stackblitz example I noticed that you're doing:
interface Payload2 { a: string; b?: string; } const payload2: Payload2 = { a: 'a', b: 'b' }; logger.info('not working', payload2);
One of the following instead should work:
type Payload2 = Record<'a'|'b', string>; const payload2: Payload2 = { a: 'a', b: 'b' }; logger.info('not working', payload2);
interface Payload2 { [key: string]: string; } const payload2: Payload2 = { a: 'a', b: 'b' }; logger.info('not working', payload2);
The problem with the original was that typescript couldn't determine the type of the index, the Context type needs it to be defined as
string
. I realize that these are not the same though and I'm not sure if there's another way to mark the index as string in Typescript while preserving the original interface 😕 .
from logtail-js.
Thank you for the quick follow up.
- I do think it improves the DX greatly if logtail could handle
unknown
internally, so that we (the users) wouldn't have to doerr as Error
orif (err instanceof Error)
everywhere where we catch an error and want to log it.
Also, a lot of projects restricts type assertion with eslint, and we'd have to add disable eslint comments everywhere. This makes it rather cluttered IMO.
try {
...
} catch (err) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
logger.error(err as Error);
}
In the code within logtail where you sanitize the log data, you could do something like:
if (value instanceof Error) {
return value.message; // potentially do something with value.stack to log error stack trace?
}
For other unknown
values that logtail is unable to parse, I'd say they can be ignored as it's unlikely that we'd be able to do anything with it or be interested anyway.
Maybe even typing Context as Record<string, unknown>
with logtail parsing/sanitizing everything that it can could work? So basically extending the parsing to handle arrays, etc., so that we can log anything with logtail doing it's best effort to handle it
- I do know that there are other ways of typing objects in Typescript so that they can be logged with logtail. However, a lot of Typescript projects use
undefined
extensively on their interfaces and types. This makes it cumbersome to work with logtail.
Typing an object like the following is a very common way in Typescript for defining optional fields:
interface Address {
id: string;
town: string;
country: string;
postal_code: string;
state?: string;
floor?: number;
}
Not being able to log these objects is a showstopper, so I definitely think it should be supported.
In one project I work on we use the following code snippet, which could perhaps be of inspiration:
// https://github.com/apollographql/apollo-client/issues/2412#issuecomment-755449680
type RecursivelyReplaceNullWithUndefined<T> = T extends null
? undefined
: {
[K in keyof T]: T[K] extends (infer U)[]
? RecursivelyReplaceNullWithUndefined<U>[]
: RecursivelyReplaceNullWithUndefined<T[K]>;
};
/**
* Recursively removes all null and undefined properties of an object
* @param input Some object
* @returns Cleaned object (without null and undefined properties)
*/
export function omitNullishValues<T extends object>(
input: T
): RecursivelyReplaceNullWithUndefined<T> {
// See https://stackoverflow.com/questions/37246775/
const omitByRecursively = (value: object, iteratee: (value: string, key: string) => boolean) => {
const cb = (v: Record<string, unknown>): unknown => omitByRecursively(v, iteratee);
return value.constructor.name === 'Object' || value.constructor.name === 'Array'
? lodash.isArray(value)
? lodash.map(value, cb)
: lodash(value).omitBy(iteratee).mapValues(cb).value()
: value;
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return omitByRecursively(
input,
value => value === null || value === undefined
) as RecursivelyReplaceNullWithUndefined<T>;
}
Let me know if there's anything I can do to help.
from logtail-js.
Hi @pimme,
thanks for opening the issue and providing the details. We originally thought strongly typed context would help devs to know what kinds of types can be safely used, but the added complexity of use is really not worth it.
Since v0.4.6
the work with context is much more streamlined 👍 The only difference from your demo is that passing err: unknown
directly as log message is not allowed. You can pass it as a context though (logger.error("Flux capacitor malfunctioned", err)
).
I've also improved working with errors and unserializable types (eg. functions), and same process is used in Browser logging, if you're using it 🙂
Thank you for your patience 🙏 Feel free to reopen the issue if something feels off or you have any question.
from logtail-js.
Related Issues (20)
- Document releases HOT 1
- Handle circular reference stringifying HOT 4
- Version 0.3.0 break lib. HOT 2
- Add data scrubbing for sensitive fields HOT 1
- Uncaught ReferenceError: process is not defined HOT 1
- Cannot find module '@babel/generator' HOT 2
- Question: API query helper HOT 1
- Connection error throws and breaks execution HOT 5
- Invalid peer dependency specified in logtail/winston package HOT 2
- Unauthorized error on logs HOT 4
- Error logging with 0.4.0 HOT 3
- `await logtail.flush()` hangs (does not properly resolve) in v0.4.0 HOT 3
- support for edge environments (Cloudflare Workers, Vercel Serverless, etc) HOT 7
- feature request: sourcemap support HOT 2
- Add ability to color log outputs HOT 6
- How to override behavior for localhost HOT 4
- Logtail blocked by adblocker HOT 3
- MaxListenersExceededWarning: Possible EventEmitter memory leak detected. HOT 4
- Logtail fails gh-action workflows on a javascript boilerplate HOT 3
- Disable @logtail/edge's warning about missing executionCtx when the runtime is not Cloudflare HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logtail-js.