[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-wallet-core] branch master updated: log to stderr when running on
From: |
gnunet |
Subject: |
[taler-wallet-core] branch master updated: log to stderr when running on node |
Date: |
Thu, 26 Mar 2020 16:05:16 +0100 |
This is an automated email from the git hooks/post-receive script.
dold pushed a commit to branch master
in repository wallet-core.
The following commit(s) were added to refs/heads/master by this push:
new 3d0fbea4 log to stderr when running on node
3d0fbea4 is described below
commit 3d0fbea4671f5a4a35f29445fbf10e3138f49229
Author: Florian Dold <address@hidden>
AuthorDate: Thu Mar 26 20:35:06 2020 +0530
log to stderr when running on node
---
src/util/logging.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++++++----
src/webex/compat.ts | 7 ++++++
2 files changed, 68 insertions(+), 5 deletions(-)
diff --git a/src/util/logging.ts b/src/util/logging.ts
index 4560105f..80ba344d 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
+}
diff --git a/src/webex/compat.ts b/src/webex/compat.ts
index 65ddfab4..121c58e7 100644
--- a/src/webex/compat.ts
+++ b/src/webex/compat.ts
@@ -26,3 +26,10 @@ export function isFirefox(): boolean {
}
return false;
}
+
+/**
+ * Check if we are running under nodejs.
+ */
+export function isNode() {
+ return (typeof process !== 'undefined') && (process.release.name ===
'node')
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
address@hidden.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [taler-wallet-core] branch master updated: log to stderr when running on node,
gnunet <=