log.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. import {Logger} from 'ts-log';
  18. import {ConsoleLogger, LevelLogger, MemoryLogger, TagLogger, TeeLogger, UnveilLogger} from '../helpers/logger';
  19. import LogLevel = threema.LogLevel;
  20. /**
  21. * Initialises logging and hands out Logger instances.
  22. */
  23. export class LogService {
  24. private readonly config: threema.Config;
  25. public readonly memory: MemoryLogger;
  26. private readonly root: TeeLogger;
  27. public static $inject = ['CONFIG'];
  28. constructor(config: threema.Config) {
  29. this.config = config;
  30. // Initialise root logger
  31. let logger: Logger;
  32. const loggers: Logger[] = [];
  33. // Initialise console logging
  34. logger = new ConsoleLogger();
  35. logger = new UnveilLogger(logger);
  36. if (config.CONSOLE_LOG_LEVEL !== 'debug') {
  37. logger = new LevelLogger(logger, config.CONSOLE_LOG_LEVEL);
  38. }
  39. loggers.push(logger);
  40. // Initialise memory logging
  41. logger = this.memory = new MemoryLogger(config.REPORT_LOG_LIMIT);
  42. if (config.REPORT_LOG_LEVEL !== 'debug') {
  43. logger = new LevelLogger(logger, config.REPORT_LOG_LEVEL);
  44. }
  45. loggers.push(logger);
  46. // Initialise tee logging
  47. this.root = new TeeLogger(loggers);
  48. }
  49. /**
  50. * Get a logger.
  51. * @param tag The tag prefix for the logger.
  52. * @param style Optional CSS style to be included with the tag.
  53. * @param level An optional level to be used. Note that loggers higher up
  54. * in the chain will supersede filtering of the log level. Thus, it is
  55. * possible to reduce the amount of logged levels but not increase them.
  56. */
  57. public getLogger(tag: string, style?: string, level?: LogLevel): Logger {
  58. // Style the tag
  59. let styledTag: string;
  60. if (style !== undefined) {
  61. styledTag = `%c[${tag}]`;
  62. } else {
  63. styledTag = `[${tag}]`;
  64. }
  65. // Pad the styled tag
  66. styledTag = styledTag.padStart(this.config.LOG_TAG_PADDING + styledTag.length - tag.length);
  67. // Create logger instance
  68. let logger: Logger;
  69. if (style !== undefined) {
  70. logger = new TagLogger(this.root, styledTag, style);
  71. } else {
  72. logger = new TagLogger(this.root, styledTag);
  73. }
  74. if (level !== undefined) {
  75. logger = new LevelLogger(logger, level);
  76. }
  77. return logger;
  78. }
  79. }