Greasy Fork is available in English.

CKLogger

A inheritable and subscribable logger util.

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greasyfork.org/scripts/441386/1027348/CKLogger.js

  1. // ==UserScript==
  2. // @name CKLogger
  3. // @namespace ckylin-script-lib-logger
  4. // @version 1.0
  5. // @match http://*
  6. // @match https://*
  7. // @author CKylinMC
  8. // @license GPLv3 License
  9. // @grant none
  10. // ==/UserScript==
  11. (function(){
  12. if(typeof(this.unsafeWindow)==='undefined'){
  13. unsafeWindow = window;
  14. }
  15. class Logger{
  16. static getStack(){
  17. return new Error().stack.substr(7);
  18. }
  19. constructor(loggerName='LOG', parent=null){
  20. this.name = loggerName;
  21. this.parent = parent;
  22. this.visible = this.parent?null:true;
  23. this.serviceProvider = console;
  24. this.subscribers = [];
  25. }
  26. trigger(opt){
  27. this.subscribers.filter(subscriber=>{
  28. if(!this.isVisible() || !opt.visible){
  29. if(subscriber.onlyVisible)return false;
  30. }
  31. if(opt.fromSub){
  32. if(!subscriber.includeSubLoggers)return false;
  33. }
  34. return subscriber.levels.includes(opt.level);
  35. }).forEach(subscriber=>{
  36. try{
  37. subscriber.onlog(opt.level,opt.name,...opt.args);
  38. }catch(e){}
  39. });
  40. if(this.parent){
  41. opt.fromSub = true;
  42. this.parent.trigger(opt);
  43. }
  44. }
  45. subscribe(options){
  46. const opt = Object.assign({
  47. onlyVisible: false,
  48. includeSubLoggers: true,
  49. levels: ['log','info','error','warn','debug'],
  50. onlog: (level,namespace,...args)=>{}
  51. },options);
  52. this.subscribers.push(opt);
  53. return opt;
  54. }
  55. unsubscribe(obj){
  56. if(this.subscribers.includes(obj)){
  57. return this.subscribers.splice(this.subscribers.indexOf(obj),1);
  58. } return null;
  59. }
  60. isVisible(){
  61. if(this.visible===null){
  62. if(this.parent&&this.parent.isVisible){
  63. return this.parent.isVisible();
  64. } else return true;
  65. }
  66. return this.visible;
  67. }
  68. setVisible(yes=true){
  69. this.visible = yes;
  70. }
  71. getSubLogger(subName=null){
  72. return new Logger(subName, this);
  73. }
  74. getName(){
  75. return `${this.parent?this.parent.getName()+'/':''}${this.name}`;
  76. }
  77. getFormattedName(){
  78. return `[${this.getName()}]`
  79. }
  80. sendLogs(subMethod,...contents){
  81. this.trigger({
  82. level: 'raw',
  83. fromSub: false,
  84. visible: this.isVisible(),
  85. name: this.getName(),
  86. args: contents
  87. });
  88. if(this.isVisible()){
  89. if(this.serviceProvider.hasOwnProperty(subMethod)){
  90. this.serviceProvider[subMethod](...contents);
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. log(...args){
  97. this.trigger({
  98. level: 'log',
  99. fromSub: false,
  100. visible: this.isVisible(),
  101. name: this.getName(),
  102. args: args
  103. });
  104. return this.sendLogs('log',this.getFormattedName(),...args);
  105. }
  106. info(...args){
  107. this.trigger({
  108. level: 'info',
  109. fromSub: false,
  110. visible: this.isVisible(),
  111. name: this.getName(),
  112. args: args
  113. });
  114. return this.sendLogs('info',this.getFormattedName(),...args);
  115. }
  116. warn(...args){
  117. this.trigger({
  118. level: 'warn',
  119. fromSub: false,
  120. visible: this.isVisible(),
  121. name: this.getName(),
  122. args: args
  123. });
  124. const stacks = '\nTrace:\n'+Logger.getStack().split('\n').splice(2).join('\n');
  125. return this.sendLogs('warn',this.getFormattedName(),...args,stacks);
  126. }
  127. error(...args){
  128. this.trigger({
  129. level: 'error',
  130. fromSub: false,
  131. visible: this.isVisible(),
  132. name: this.getName(),
  133. args: args
  134. });
  135. const stacks = '\nTrace:\n'+Logger.getStack().split('\n').splice(2).join('\n');
  136. return this.sendLogs('error',this.getFormattedName(),...args,stacks);
  137. }
  138. debug(...args){
  139. this.trigger({
  140. level: 'debug',
  141. fromSub: false,
  142. visible: this.isVisible(),
  143. name: this.getName(),
  144. args: args
  145. });
  146. const stacks = '\nTrace:\n'+Logger.getStack().split('\n').splice(2).join('\n');
  147. return this.sendLogs('debug',this.getFormattedName(),...args,stacks);
  148. }
  149. send(methodName,...args){
  150. this.trigger({
  151. level: methodName,
  152. fromSub: false,
  153. visible: this.isVisible(),
  154. name: this.getName(),
  155. args: args
  156. });
  157. return this.sendLogs(methodName,this.getFormattedName(),...args);
  158. }
  159. }
  160. unsafeWindow.Logger=Logger;
  161. unsafeWindow.logger=new Logger;
  162. })();