summaryrefslogtreecommitdiff
path: root/src/util/logging.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-03-26 20:35:06 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-03-26 20:35:06 +0530
commit3d0fbea4671f5a4a35f29445fbf10e3138f49229 (patch)
tree14dcaa68f62b049583b14e0b9822d67fa479e43b /src/util/logging.ts
parentccdc7cffed3ba784c8fd65e0779cd0807a53d39f (diff)
downloadwallet-core-3d0fbea4671f5a4a35f29445fbf10e3138f49229.tar.gz
wallet-core-3d0fbea4671f5a4a35f29445fbf10e3138f49229.tar.bz2
wallet-core-3d0fbea4671f5a4a35f29445fbf10e3138f49229.zip
log to stderr when running on node
Diffstat (limited to 'src/util/logging.ts')
-rw-r--r--src/util/logging.ts66
1 files changed, 61 insertions, 5 deletions
diff --git a/src/util/logging.ts b/src/util/logging.ts
index 4560105f4..80ba344d5 100644
--- a/src/util/logging.ts
+++ b/src/util/logging.ts
@@ -14,18 +14,74 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
+/**
+ * Imports.
+ */
+import { isNode } from "../webex/compat";
+
+function writeNodeLog(
+ message: string,
+ tag: string,
+ level: string,
+ args: any[],
+) {
+ process.stderr.write(`${new Date().toISOString()} ${tag} ${level} `);
+ process.stderr.write(message);
+ if (args.length != 0) {
+ process.stderr.write(" ");
+ process.stderr.write(JSON.stringify(args, undefined, 2));
+ }
+ process.stderr.write("\n");
+}
+
+/**
+ * Logger that writes to stderr when running under node,
+ * and uses the corresponding console.* method to log in the browser.
+ */
export class Logger {
constructor(private tag: string) {}
+
info(message: string, ...args: any[]) {
- console.log(`${new Date().toISOString()} ${this.tag} INFO ` + message, ...args);
+ if (isNode()) {
+ writeNodeLog(message, this.tag, "INFO", args);
+ } else {
+ console.info(
+ `${new Date().toISOString()} ${this.tag} INFO ` + message,
+ ...args,
+ );
+ }
}
+
warn(message: string, ...args: any[]) {
- console.log(`${new Date().toISOString()} ${this.tag} WARN ` + message, ...args);
+ if (isNode()) {
+ writeNodeLog(message, this.tag, "WARN", args);
+ } else {
+ console.warn(
+ `${new Date().toISOString()} ${this.tag} INFO ` + message,
+ ...args,
+ );
+ }
}
+
error(message: string, ...args: any[]) {
- console.log(`${new Date().toISOString()} ${this.tag} ERROR ` + message, ...args);
+ if (isNode()) {
+ writeNodeLog(message, this.tag, "ERROR", args);
+ } else {
+ console.info(
+ `${new Date().toISOString()} ${this.tag} ERROR ` + message,
+ ...args,
+ );
+ }
}
+
trace(message: any, ...args: any[]) {
- console.log(`${new Date().toISOString()} ${this.tag} TRACE ` + message, ...args)
+ if (isNode()) {
+ writeNodeLog(message, this.tag, "TRACE", args);
+ } else {
+ console.info(
+ `${new Date().toISOString()} ${this.tag} TRACE ` + message,
+ ...args,
+ );
+ }
}
-} \ No newline at end of file
+}