I'm currently working on refining my log method to be more versatile. The main concept I'm aiming for is outlined below:
class A {
user_id: number;
// and other properties...
}
class B {
service_name: string;
// and other properties...
}
function log<T>(...T parameters) {
// log the input parameters
}
log<A>(
A.user_id = 123
);
log<B>(
B.service_name = 'my_service_name';
);
My attempts so far have been unsuccessful in achieving this, but it's important as my project continues to expand with more layers and entries added each day.
enum levels {
error,
info,
}
enum entries {
user_id,
service_name,
}
function log(level: levels, ...entries: entries[]) {
// carry out the logging operation...
}
log(
levels.info,
entries.user_id = 123
);
log(
levels.error,
entries.service_name = 'my_sevice_name'
);
I decided to utilize enums in my example to guide developers towards choosing predefined values from a list rather than resorting to magic numbers or strings.