homegames-common 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +44 -26
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -664,42 +664,60 @@ const msgToString = (msg) => {
664
664
  return typeof msg === 'object' ? JSON.stringify(msg) : msg;
665
665
  };
666
666
 
667
+ let electronLogger = null;
668
+
669
+ try {
670
+ require.resolve('electron-log');
671
+ electronLogger = require('electron-log');
672
+ } catch (err) {
673
+ console.log('Logger not using electron. Logging to file.');
674
+ }
675
+
676
+
667
677
  const log = {
668
678
  info: (msg, explanation = null) => {
669
- const logLevel = getLogLevel();
670
- const required = getLogLevel('INFO');
679
+ if (electronLogger) {
680
+ electronLogger.info(msgToString(msg));
681
+ } else {
682
+ const logLevel = getLogLevel();
683
+ const required = getLogLevel('INFO');
671
684
 
672
- if (logLevel < required) {
673
- return;
674
- }
685
+ if (logLevel < required) {
686
+ return;
687
+ }
675
688
 
676
- const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
689
+ const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
677
690
 
678
- const msgString = `[HOMEGAMES-INFO][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
679
- fs.appendFile(logPath, msgString, (err) => {
680
- if (err) {
681
- console.error('failed log');
682
- console.log(err);
683
- }
684
- });
691
+ const msgString = `[HOMEGAMES-INFO][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
692
+ fs.appendFile(logPath, msgString, (err) => {
693
+ if (err) {
694
+ console.error('failed log');
695
+ console.log(err);
696
+ }
697
+ });
698
+ }
685
699
  },
686
700
  error: (msg, explanation) => {
687
- const logLevel = getLogLevel();
688
- const required = getLogLevel('INFO');
701
+ if (electronLogger) {
702
+ electronLogger.error(msgToString(msg));
703
+ } else {
704
+ const logLevel = getLogLevel();
705
+ const required = getLogLevel('INFO');
689
706
 
690
- if (logLevel < required) {
691
- return;
692
- }
707
+ if (logLevel < required) {
708
+ return;
709
+ }
693
710
 
694
- const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
711
+ const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
695
712
 
696
- const msgString = `[HOMEGAMES-ERROR][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
697
- fs.appendFile(logPath, msgString, (err) => {
698
- if (err) {
699
- console.error('failed log');
700
- console.log(err);
701
- }
702
- });
713
+ const msgString = `[HOMEGAMES-ERROR][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
714
+ fs.appendFile(logPath, msgString, (err) => {
715
+ if (err) {
716
+ console.error('failed log');
717
+ console.log(err);
718
+ }
719
+ });
720
+ }
703
721
  },
704
722
  debug: (msg, explanation) => {
705
723
  const logLevel = getLogLevel();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homegames-common",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Homegames common tools",
5
5
  "main": "index.js",
6
6
  "scripts": {