log.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import path from "path"
  2. import fs from "fs/promises"
  3. import { Global } from "../global"
  4. import z from "zod"
  5. export namespace Log {
  6. export const Level = z.enum(["DEBUG", "INFO", "WARN", "ERROR"]).meta({ ref: "LogLevel", description: "Log level" })
  7. export type Level = z.infer<typeof Level>
  8. const levelPriority: Record<Level, number> = {
  9. DEBUG: 0,
  10. INFO: 1,
  11. WARN: 2,
  12. ERROR: 3,
  13. }
  14. let level: Level = "INFO"
  15. function shouldLog(input: Level): boolean {
  16. return levelPriority[input] >= levelPriority[level]
  17. }
  18. export type Logger = {
  19. debug(message?: any, extra?: Record<string, any>): void
  20. info(message?: any, extra?: Record<string, any>): void
  21. error(message?: any, extra?: Record<string, any>): void
  22. warn(message?: any, extra?: Record<string, any>): void
  23. tag(key: string, value: string): Logger
  24. clone(): Logger
  25. time(
  26. message: string,
  27. extra?: Record<string, any>,
  28. ): {
  29. stop(): void
  30. [Symbol.dispose](): void
  31. }
  32. }
  33. const loggers = new Map<string, Logger>()
  34. export const Default = create({ service: "default" })
  35. export interface Options {
  36. print: boolean
  37. dev?: boolean
  38. level?: Level
  39. }
  40. let logpath = ""
  41. export function file() {
  42. return logpath
  43. }
  44. let write = (msg: any) => {
  45. process.stderr.write(msg)
  46. return msg.length
  47. }
  48. export async function init(options: Options) {
  49. if (options.level) level = options.level
  50. cleanup(Global.Path.log)
  51. if (options.print) return
  52. logpath = path.join(
  53. Global.Path.log,
  54. options.dev ? "dev.log" : new Date().toISOString().split(".")[0].replace(/:/g, "") + ".log",
  55. )
  56. const logfile = Bun.file(logpath)
  57. await fs.truncate(logpath).catch(() => {})
  58. const writer = logfile.writer()
  59. write = async (msg: any) => {
  60. const num = writer.write(msg)
  61. writer.flush()
  62. return num
  63. }
  64. }
  65. async function cleanup(dir: string) {
  66. const glob = new Bun.Glob("????-??-??T??????.log")
  67. const files = await Array.fromAsync(
  68. glob.scan({
  69. cwd: dir,
  70. absolute: true,
  71. }),
  72. )
  73. if (files.length <= 5) return
  74. const filesToDelete = files.slice(0, -10)
  75. await Promise.all(filesToDelete.map((file) => fs.unlink(file).catch(() => {})))
  76. }
  77. function formatError(error: Error, depth = 0): string {
  78. const result = error.message
  79. return error.cause instanceof Error && depth < 10
  80. ? result + " Caused by: " + formatError(error.cause, depth + 1)
  81. : result
  82. }
  83. let last = Date.now()
  84. export function create(tags?: Record<string, any>) {
  85. tags = tags || {}
  86. const service = tags["service"]
  87. if (service && typeof service === "string") {
  88. const cached = loggers.get(service)
  89. if (cached) {
  90. return cached
  91. }
  92. }
  93. function build(message: any, extra?: Record<string, any>) {
  94. const prefix = Object.entries({
  95. ...tags,
  96. ...extra,
  97. })
  98. .filter(([_, value]) => value !== undefined && value !== null)
  99. .map(([key, value]) => {
  100. const prefix = `${key}=`
  101. if (value instanceof Error) return prefix + formatError(value)
  102. if (typeof value === "object") return prefix + JSON.stringify(value)
  103. return prefix + value
  104. })
  105. .join(" ")
  106. const next = new Date()
  107. const diff = next.getTime() - last
  108. last = next.getTime()
  109. return [next.toISOString().split(".")[0], "+" + diff + "ms", prefix, message].filter(Boolean).join(" ") + "\n"
  110. }
  111. const result: Logger = {
  112. debug(message?: any, extra?: Record<string, any>) {
  113. if (shouldLog("DEBUG")) {
  114. write("DEBUG " + build(message, extra))
  115. }
  116. },
  117. info(message?: any, extra?: Record<string, any>) {
  118. if (shouldLog("INFO")) {
  119. write("INFO " + build(message, extra))
  120. }
  121. },
  122. error(message?: any, extra?: Record<string, any>) {
  123. if (shouldLog("ERROR")) {
  124. write("ERROR " + build(message, extra))
  125. }
  126. },
  127. warn(message?: any, extra?: Record<string, any>) {
  128. if (shouldLog("WARN")) {
  129. write("WARN " + build(message, extra))
  130. }
  131. },
  132. tag(key: string, value: string) {
  133. if (tags) tags[key] = value
  134. return result
  135. },
  136. clone() {
  137. return Log.create({ ...tags })
  138. },
  139. time(message: string, extra?: Record<string, any>) {
  140. const now = Date.now()
  141. result.info(message, { status: "started", ...extra })
  142. function stop() {
  143. result.info(message, {
  144. status: "completed",
  145. duration: Date.now() - now,
  146. ...extra,
  147. })
  148. }
  149. return {
  150. stop,
  151. [Symbol.dispose]() {
  152. stop()
  153. },
  154. }
  155. },
  156. }
  157. if (service && typeof service === "string") {
  158. loggers.set(service, result)
  159. }
  160. return result
  161. }
  162. }