@typespec/ts-http-runtime 0.2.3-alpha.20250529.3 → 0.2.3-alpha.20250603.2

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.
@@ -30,10 +30,23 @@ export function createLoggerContext(options) {
30
30
  clientLogger.log = (...args) => {
31
31
  debug.log(...args);
32
32
  };
33
+ function contextSetLogLevel(level) {
34
+ if (level && !isTypeSpecRuntimeLogLevel(level)) {
35
+ throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
36
+ }
37
+ logLevel = level;
38
+ const enabledNamespaces = [];
39
+ for (const logger of registeredLoggers) {
40
+ if (shouldEnable(logger)) {
41
+ enabledNamespaces.push(logger.namespace);
42
+ }
43
+ }
44
+ debug.enable(enabledNamespaces.join(","));
45
+ }
33
46
  if (logLevelFromEnv) {
34
47
  // avoid calling setLogLevel because we don't want a mis-set environment variable to crash
35
48
  if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {
36
- setLogLevel(logLevelFromEnv);
49
+ contextSetLogLevel(logLevelFromEnv);
37
50
  }
38
51
  else {
39
52
  console.error(`${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(", ")}.`);
@@ -54,33 +67,23 @@ export function createLoggerContext(options) {
54
67
  registeredLoggers.add(logger);
55
68
  return logger;
56
69
  }
70
+ function contextGetLogLevel() {
71
+ return logLevel;
72
+ }
73
+ function contextCreateClientLogger(namespace) {
74
+ const clientRootLogger = clientLogger.extend(namespace);
75
+ patchLogMethod(clientLogger, clientRootLogger);
76
+ return {
77
+ error: createLogger(clientRootLogger, "error"),
78
+ warning: createLogger(clientRootLogger, "warning"),
79
+ info: createLogger(clientRootLogger, "info"),
80
+ verbose: createLogger(clientRootLogger, "verbose"),
81
+ };
82
+ }
57
83
  return {
58
- setLogLevel(level) {
59
- if (level && !isTypeSpecRuntimeLogLevel(level)) {
60
- throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
61
- }
62
- logLevel = level;
63
- const enabledNamespaces = [];
64
- for (const logger of registeredLoggers) {
65
- if (shouldEnable(logger)) {
66
- enabledNamespaces.push(logger.namespace);
67
- }
68
- }
69
- debug.enable(enabledNamespaces.join(","));
70
- },
71
- getLogLevel() {
72
- return logLevel;
73
- },
74
- createClientLogger(namespace) {
75
- const clientRootLogger = clientLogger.extend(namespace);
76
- patchLogMethod(clientLogger, clientRootLogger);
77
- return {
78
- error: createLogger(clientRootLogger, "error"),
79
- warning: createLogger(clientRootLogger, "warning"),
80
- info: createLogger(clientRootLogger, "info"),
81
- verbose: createLogger(clientRootLogger, "verbose"),
82
- };
83
- },
84
+ setLogLevel: contextSetLogLevel,
85
+ getLogLevel: contextGetLogLevel,
86
+ createClientLogger: contextCreateClientLogger,
84
87
  logger: clientLogger,
85
88
  };
86
89
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,KAAK,MAAM,YAAY,CAAC;AAiG/B,MAAM,2BAA2B,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAI5E,MAAM,QAAQ,GAAG;IACf,OAAO,EAAE,GAAG;IACZ,IAAI,EAAE,GAAG;IACT,OAAO,EAAE,GAAG;IACZ,KAAK,EAAE,GAAG;CACX,CAAC;AAEF,SAAS,cAAc,CACrB,MAAmC,EACnC,KAAyD;IAEzD,KAAK,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,EAAE,EAAE;QACtB,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;IACtB,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,yBAAyB,CAAC,KAAa;IAC9C,OAAO,2BAA2B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BAAgB,EAAE,MAAM,CAAC;gBAC5C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;aACnD,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,YAAY;KACrB,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,GAAG,mBAAmB,CAAC;IAClC,kBAAkB,EAAE,4BAA4B;IAChD,SAAS,EAAE,iBAAiB;CAC7B,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,2DAA2D;AAC3D,MAAM,CAAC,MAAM,qBAAqB,GAAgC,OAAO,CAAC,MAAM,CAAC;AAEjF;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,QAAkC;IAC5D,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,SAAiB;IAClD,OAAO,OAAO,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n setLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n return {\n setLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n },\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n },\n createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n },\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,KAAK,MAAM,YAAY,CAAC;AAiG/B,MAAM,2BAA2B,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAI5E,MAAM,QAAQ,GAAG;IACf,OAAO,EAAE,GAAG;IACZ,IAAI,EAAE,GAA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AED,SAAS,yBAAyB,CAAC,SAAiB;QAClD,MAAM,gBAAgB,GAAgC,YAAY,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACrF,cAAc,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;QAC/C,OAAO;YACL,KAAK,EAAE,YAAY,CAAC,gBAAgB,EAAE,OAAO,CAAC;YAC9C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;YAClD,IAAI,EAAE,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC;YAC5C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;SACnD,CAAC;IACJ,CAAC;IAED,OAAO;QACL,WAAW,EAAE,kBAAkB;QAC/B,WAAW,EAAE,kBAAkB;QAC/B,kBAAkB,EAAE,yBAAyB;QAC7C,MAAM,EAAE,YAAY;KACrB,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,GAAG,mBAAmB,CAAC;IAClC,kBAAkB,EAAE,4BAA4B;IAChD,SAAS,EAAE,iBAAiB;CAC7B,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,2DAA2D;AAC3D,MAAM,CAAC,MAAM,qBAAqB,GAAgC,OAAO,CAAC,MAAM,CAAC;AAEjF;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,QAAkC;IAC5D,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,SAAiB;IAClD,OAAO,OAAO,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n function contextSetLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n }\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n contextSetLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n function contextGetLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n }\n\n function contextCreateClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n }\n\n return {\n setLogLevel: contextSetLogLevel,\n getLogLevel: contextGetLogLevel,\n createClientLogger: contextCreateClientLogger,\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
@@ -38,10 +38,23 @@ function createLoggerContext(options) {
38
38
  clientLogger.log = (...args) => {
39
39
  debug_js_1.default.log(...args);
40
40
  };
41
+ function contextSetLogLevel(level) {
42
+ if (level && !isTypeSpecRuntimeLogLevel(level)) {
43
+ throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
44
+ }
45
+ logLevel = level;
46
+ const enabledNamespaces = [];
47
+ for (const logger of registeredLoggers) {
48
+ if (shouldEnable(logger)) {
49
+ enabledNamespaces.push(logger.namespace);
50
+ }
51
+ }
52
+ debug_js_1.default.enable(enabledNamespaces.join(","));
53
+ }
41
54
  if (logLevelFromEnv) {
42
55
  // avoid calling setLogLevel because we don't want a mis-set environment variable to crash
43
56
  if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {
44
- setLogLevel(logLevelFromEnv);
57
+ contextSetLogLevel(logLevelFromEnv);
45
58
  }
46
59
  else {
47
60
  console.error(`${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(", ")}.`);
@@ -62,33 +75,23 @@ function createLoggerContext(options) {
62
75
  registeredLoggers.add(logger);
63
76
  return logger;
64
77
  }
78
+ function contextGetLogLevel() {
79
+ return logLevel;
80
+ }
81
+ function contextCreateClientLogger(namespace) {
82
+ const clientRootLogger = clientLogger.extend(namespace);
83
+ patchLogMethod(clientLogger, clientRootLogger);
84
+ return {
85
+ error: createLogger(clientRootLogger, "error"),
86
+ warning: createLogger(clientRootLogger, "warning"),
87
+ info: createLogger(clientRootLogger, "info"),
88
+ verbose: createLogger(clientRootLogger, "verbose"),
89
+ };
90
+ }
65
91
  return {
66
- setLogLevel(level) {
67
- if (level && !isTypeSpecRuntimeLogLevel(level)) {
68
- throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
69
- }
70
- logLevel = level;
71
- const enabledNamespaces = [];
72
- for (const logger of registeredLoggers) {
73
- if (shouldEnable(logger)) {
74
- enabledNamespaces.push(logger.namespace);
75
- }
76
- }
77
- debug_js_1.default.enable(enabledNamespaces.join(","));
78
- },
79
- getLogLevel() {
80
- return logLevel;
81
- },
82
- createClientLogger(namespace) {
83
- const clientRootLogger = clientLogger.extend(namespace);
84
- patchLogMethod(clientLogger, clientRootLogger);
85
- return {
86
- error: createLogger(clientRootLogger, "error"),
87
- warning: createLogger(clientRootLogger, "warning"),
88
- info: createLogger(clientRootLogger, "info"),
89
- verbose: createLogger(clientRootLogger, "verbose"),
90
- };
91
- },
92
+ setLogLevel: contextSetLogLevel,
93
+ getLogLevel: contextGetLogLevel,
94
+ createClientLogger: contextCreateClientLogger,
92
95
  logger: clientLogger,
93
96
  };
94
97
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAgIlC,kDAmFC;AAsBD,kCAEC;AAKD,kCAEC;AAOD,gDAEC;;AAzPD,kEAA+B;AAiG/B,MAAM,2BAA2B,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAI5E,MAAM,QAAQ,GAAG;IACf,OAAO,EAAE,GAAG;IACZ,IAAI,EAAE,GAAG;IACT,OAAO,EAAE,GAAG;IACZ,KAAK,EAAE,GAAG;CACX,CAAC;AAEF,SAAS,cAAc,CACrB,MAAmC,EACnC,KAAyD;IAEzD,KAAK,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,EAAE,EAAE;QACtB,MAAM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E,YAAY,CAAC,gBAAgB,EAAE,OAAO,CAAC;gBAC9C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;gBAClD,IAAI,EAAE,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC;gBAC5C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;aACnD,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,YAAY;KACrB,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,GAAG,mBAAmB,CAAC;IAClC,kBAAkB,EAAE,4BAA4B;IAChD,SAAS,EAAE,iBAAiB;CAC7B,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,2DAA2D;AAC9C,QAAA,qBAAqB,GAAgC,OAAO,CAAC,MAAM,CAAC;AAEjF;;GAEG;AACH,SAAgB,WAAW,CAAC,QAAkC;IAC5D,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,SAAgB,WAAW;IACzB,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,SAAgB,kBAAkB,CAAC,SAAiB;IAClD,OAAO,OAAO,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n setLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n return {\n setLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n },\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n },\n createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n },\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAgIlC,kDAyFC;AAsBD,kCAEC;AAKD,kCAEC;AAOD,gDAEC;;AA/PD,kEAA+B;AAiG/B,MAAM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["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n function contextSetLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n }\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n contextSetLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n function contextGetLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n }\n\n function contextCreateClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n }\n\n return {\n setLogLevel: contextSetLogLevel,\n getLogLevel: contextGetLogLevel,\n createClientLogger: contextCreateClientLogger,\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
@@ -30,10 +30,23 @@ export function createLoggerContext(options) {
30
30
  clientLogger.log = (...args) => {
31
31
  debug.log(...args);
32
32
  };
33
+ function contextSetLogLevel(level) {
34
+ if (level && !isTypeSpecRuntimeLogLevel(level)) {
35
+ throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
36
+ }
37
+ logLevel = level;
38
+ const enabledNamespaces = [];
39
+ for (const logger of registeredLoggers) {
40
+ if (shouldEnable(logger)) {
41
+ enabledNamespaces.push(logger.namespace);
42
+ }
43
+ }
44
+ debug.enable(enabledNamespaces.join(","));
45
+ }
33
46
  if (logLevelFromEnv) {
34
47
  // avoid calling setLogLevel because we don't want a mis-set environment variable to crash
35
48
  if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {
36
- setLogLevel(logLevelFromEnv);
49
+ contextSetLogLevel(logLevelFromEnv);
37
50
  }
38
51
  else {
39
52
  console.error(`${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(", ")}.`);
@@ -54,33 +67,23 @@ export function createLoggerContext(options) {
54
67
  registeredLoggers.add(logger);
55
68
  return logger;
56
69
  }
70
+ function contextGetLogLevel() {
71
+ return logLevel;
72
+ }
73
+ function contextCreateClientLogger(namespace) {
74
+ const clientRootLogger = clientLogger.extend(namespace);
75
+ patchLogMethod(clientLogger, clientRootLogger);
76
+ return {
77
+ error: createLogger(clientRootLogger, "error"),
78
+ warning: createLogger(clientRootLogger, "warning"),
79
+ info: createLogger(clientRootLogger, "info"),
80
+ verbose: createLogger(clientRootLogger, "verbose"),
81
+ };
82
+ }
57
83
  return {
58
- setLogLevel(level) {
59
- if (level && !isTypeSpecRuntimeLogLevel(level)) {
60
- throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
61
- }
62
- logLevel = level;
63
- const enabledNamespaces = [];
64
- for (const logger of registeredLoggers) {
65
- if (shouldEnable(logger)) {
66
- enabledNamespaces.push(logger.namespace);
67
- }
68
- }
69
- debug.enable(enabledNamespaces.join(","));
70
- },
71
- getLogLevel() {
72
- return logLevel;
73
- },
74
- createClientLogger(namespace) {
75
- const clientRootLogger = clientLogger.extend(namespace);
76
- patchLogMethod(clientLogger, clientRootLogger);
77
- return {
78
- error: createLogger(clientRootLogger, "error"),
79
- warning: createLogger(clientRootLogger, "warning"),
80
- info: createLogger(clientRootLogger, "info"),
81
- verbose: createLogger(clientRootLogger, "verbose"),
82
- };
83
- },
84
+ setLogLevel: contextSetLogLevel,
85
+ getLogLevel: contextGetLogLevel,
86
+ createClientLogger: contextCreateClientLogger,
84
87
  logger: clientLogger,
85
88
  };
86
89
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,KAAK,MAAM,YAAY,CAAC;AAiG/B,MAAM,2BAA2B,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAI5E,MAAM,QAAQ,GAAG;IACf,OAAO,EAAE,GAAG;IACZ,IAAI,EAAE,GAAG;IACT,OAAO,EAAE,GAAG;IACZ,KAAK,EAAE,GAAG;CACX,CAAC;AAEF,SAAS,cAAc,CACrB,MAAmC,EACnC,KAAyD;IAEz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AAY,EAAE,gBAAgB,CAAC,CAAC;YAC/C,OAAO;gBACL,KAAK,EAAE,YAAY,CAAC,gBAAgB,EAAE,OAAO,CAAC;gBAC9C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;gBAClD,IAAI,EAAE,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC;gBAC5C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;aACnD,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,YAAY;KACrB,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,GAAG,mBAAmB,CAAC;IAClC,kBAAkB,EAAE,4BAA4B;IAChD,SAAS,EAAE,iBAAiB;CAC7B,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,2DAA2D;AAC3D,MAAM,CAAC,MAAM,qBAAqB,GAAgC,OAAO,CAAC,MAAM,CAAC;AAEjF;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,QAAkC;IAC5D,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,SAAiB;IAClD,OAAO,OAAO,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n setLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n return {\n setLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n },\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n },\n createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n },\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAEl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AAO,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n function contextSetLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n }\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n contextSetLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n function contextGetLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n }\n\n function contextCreateClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n }\n\n return {\n setLogLevel: contextSetLogLevel,\n getLogLevel: contextGetLogLevel,\n createClientLogger: contextCreateClientLogger,\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
@@ -30,10 +30,23 @@ export function createLoggerContext(options) {
30
30
  clientLogger.log = (...args) => {
31
31
  debug.log(...args);
32
32
  };
33
+ function contextSetLogLevel(level) {
34
+ if (level && !isTypeSpecRuntimeLogLevel(level)) {
35
+ throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
36
+ }
37
+ logLevel = level;
38
+ const enabledNamespaces = [];
39
+ for (const logger of registeredLoggers) {
40
+ if (shouldEnable(logger)) {
41
+ enabledNamespaces.push(logger.namespace);
42
+ }
43
+ }
44
+ debug.enable(enabledNamespaces.join(","));
45
+ }
33
46
  if (logLevelFromEnv) {
34
47
  // avoid calling setLogLevel because we don't want a mis-set environment variable to crash
35
48
  if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {
36
- setLogLevel(logLevelFromEnv);
49
+ contextSetLogLevel(logLevelFromEnv);
37
50
  }
38
51
  else {
39
52
  console.error(`${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(", ")}.`);
@@ -54,33 +67,23 @@ export function createLoggerContext(options) {
54
67
  registeredLoggers.add(logger);
55
68
  return logger;
56
69
  }
70
+ function contextGetLogLevel() {
71
+ return logLevel;
72
+ }
73
+ function contextCreateClientLogger(namespace) {
74
+ const clientRootLogger = clientLogger.extend(namespace);
75
+ patchLogMethod(clientLogger, clientRootLogger);
76
+ return {
77
+ error: createLogger(clientRootLogger, "error"),
78
+ warning: createLogger(clientRootLogger, "warning"),
79
+ info: createLogger(clientRootLogger, "info"),
80
+ verbose: createLogger(clientRootLogger, "verbose"),
81
+ };
82
+ }
57
83
  return {
58
- setLogLevel(level) {
59
- if (level && !isTypeSpecRuntimeLogLevel(level)) {
60
- throw new Error(`Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(",")}`);
61
- }
62
- logLevel = level;
63
- const enabledNamespaces = [];
64
- for (const logger of registeredLoggers) {
65
- if (shouldEnable(logger)) {
66
- enabledNamespaces.push(logger.namespace);
67
- }
68
- }
69
- debug.enable(enabledNamespaces.join(","));
70
- },
71
- getLogLevel() {
72
- return logLevel;
73
- },
74
- createClientLogger(namespace) {
75
- const clientRootLogger = clientLogger.extend(namespace);
76
- patchLogMethod(clientLogger, clientRootLogger);
77
- return {
78
- error: createLogger(clientRootLogger, "error"),
79
- warning: createLogger(clientRootLogger, "warning"),
80
- info: createLogger(clientRootLogger, "info"),
81
- verbose: createLogger(clientRootLogger, "verbose"),
82
- };
83
- },
84
+ setLogLevel: contextSetLogLevel,
85
+ getLogLevel: contextGetLogLevel,
86
+ createClientLogger: contextCreateClientLogger,
84
87
  logger: clientLogger,
85
88
  };
86
89
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,KAAK,MAAM,YAAY,CAAC;AAiG/B,MAAM,2BAA2B,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAI5E,MAAM,QAAQ,GAAG;IACf,OAAO,EAAE,GAAG;IACZ,IAAI,EAAE,GAAG;IACT,OAAO,EAAE,GAAG;IACZ,KAAK,EAAE,G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gC,YAAY,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YACrF,cAAc,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;YAC/C,OAAO;gBACL,KAAK,EAAE,YAAY,CAAC,gBAAgB,EAAE,OAAO,CAAC;gBAC9C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;gBAClD,IAAI,EAAE,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC;gBAC5C,OAAO,EAAE,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC;aACnD,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,YAAY;KACrB,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,GAAG,mBAAmB,CAAC;IAClC,kBAAkB,EAAE,4BAA4B;IAChD,SAAS,EAAE,iBAAiB;CAC7B,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,2DAA2D;AAC3D,MAAM,CAAC,MAAM,qBAAqB,GAAgC,OAAO,CAAC,MAAM,CAAC;AAEjF;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,QAAkC;IAC5D,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,SAAiB;IAClD,OAAO,OAAO,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n setLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n return {\n setLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n },\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n },\n createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n },\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":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AED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,SAAiB;IAClD,OAAO,OAAO,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport debug from \"./debug.js\";\n\nimport type { Debugger } from \"./debug.js\";\nexport type { Debugger };\n\n/**\n * The log levels supported by the logger.\n * The log levels in order of most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\nexport type TypeSpecRuntimeLogLevel = \"verbose\" | \"info\" | \"warning\" | \"error\";\n\n/**\n * A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.\n */\nexport type TypeSpecRuntimeClientLogger = Debugger;\n\n/**\n * Defines the methods available on the SDK-facing logger.\n */\nexport interface TypeSpecRuntimeLogger {\n /**\n * Used for failures the program is unlikely to recover from,\n * such as Out of Memory.\n */\n error: Debugger;\n /**\n * Used when a function fails to perform its intended task.\n * Usually this means the function will throw an exception.\n * Not used for self-healing events (e.g. automatic retry)\n */\n warning: Debugger;\n /**\n * Used when a function operates normally.\n */\n info: Debugger;\n /**\n * Used for detailed troubleshooting scenarios. This is\n * intended for use by developers / system administrators\n * for diagnosing specific failures.\n */\n verbose: Debugger;\n}\n\n/**\n * todo doc\n */\nexport interface LoggerContext {\n /**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void;\n\n /**\n * Retrieves the currently specified log level.\n */\n getLogLevel(): TypeSpecRuntimeLogLevel | undefined;\n\n /**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\n createClientLogger(namespace: string): TypeSpecRuntimeLogger;\n\n /**\n * The TypeSpecRuntimeClientLogger provides a mechanism for overriding where logs are output to.\n * By default, logs are sent to stderr.\n * Override the `log` method to redirect logs to another location.\n */\n logger: TypeSpecRuntimeClientLogger;\n}\n\n/**\n * Option for creating a TypeSpecRuntimeLoggerContext.\n */\nexport interface CreateLoggerContextOptions {\n /**\n * The name of the environment variable to check for the log level.\n */\n logLevelEnvVarName: string;\n\n /**\n * The namespace of the logger.\n */\n namespace: string;\n}\n\nconst TYPESPEC_RUNTIME_LOG_LEVELS = [\"verbose\", \"info\", \"warning\", \"error\"];\n\ntype DebuggerWithLogLevel = Debugger & { level: TypeSpecRuntimeLogLevel };\n\nconst levelMap = {\n verbose: 400,\n info: 300,\n warning: 200,\n error: 100,\n};\n\nfunction patchLogMethod(\n parent: TypeSpecRuntimeClientLogger,\n child: TypeSpecRuntimeClientLogger | DebuggerWithLogLevel,\n): void {\n child.log = (...args) => {\n parent.log(...args);\n };\n}\n\nfunction isTypeSpecRuntimeLogLevel(level: string): level is TypeSpecRuntimeLogLevel {\n return TYPESPEC_RUNTIME_LOG_LEVELS.includes(level as any);\n}\n\n/**\n * Creates a logger context base on the provided options.\n * @param options - The options for creating a logger context.\n * @returns The logger context.\n */\nexport function createLoggerContext(options: CreateLoggerContextOptions): LoggerContext {\n const registeredLoggers = new Set<DebuggerWithLogLevel>();\n const logLevelFromEnv =\n (typeof process !== \"undefined\" && process.env && process.env[options.logLevelEnvVarName]) ||\n undefined;\n\n let logLevel: TypeSpecRuntimeLogLevel | undefined;\n\n const clientLogger: TypeSpecRuntimeClientLogger = debug(options.namespace);\n clientLogger.log = (...args) => {\n debug.log(...args);\n };\n\n function contextSetLogLevel(level?: TypeSpecRuntimeLogLevel): void {\n if (level && !isTypeSpecRuntimeLogLevel(level)) {\n throw new Error(\n `Unknown log level '${level}'. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\",\")}`,\n );\n }\n logLevel = level;\n\n const enabledNamespaces = [];\n for (const logger of registeredLoggers) {\n if (shouldEnable(logger)) {\n enabledNamespaces.push(logger.namespace);\n }\n }\n\n debug.enable(enabledNamespaces.join(\",\"));\n }\n\n if (logLevelFromEnv) {\n // avoid calling setLogLevel because we don't want a mis-set environment variable to crash\n if (isTypeSpecRuntimeLogLevel(logLevelFromEnv)) {\n contextSetLogLevel(logLevelFromEnv);\n } else {\n console.error(\n `${options.logLevelEnvVarName} set to unknown log level '${logLevelFromEnv}'; logging is not enabled. Acceptable values: ${TYPESPEC_RUNTIME_LOG_LEVELS.join(\n \", \",\n )}.`,\n );\n }\n }\n\n function shouldEnable(logger: DebuggerWithLogLevel): boolean {\n return Boolean(logLevel && levelMap[logger.level] <= levelMap[logLevel]);\n }\n\n function createLogger(\n parent: TypeSpecRuntimeClientLogger,\n level: TypeSpecRuntimeLogLevel,\n ): DebuggerWithLogLevel {\n const logger: DebuggerWithLogLevel = Object.assign(parent.extend(level), {\n level,\n });\n\n patchLogMethod(parent, logger);\n\n if (shouldEnable(logger)) {\n const enabledNamespaces = debug.disable();\n debug.enable(enabledNamespaces + \",\" + logger.namespace);\n }\n\n registeredLoggers.add(logger);\n\n return logger;\n }\n\n function contextGetLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return logLevel;\n }\n\n function contextCreateClientLogger(namespace: string): TypeSpecRuntimeLogger {\n const clientRootLogger: TypeSpecRuntimeClientLogger = clientLogger.extend(namespace);\n patchLogMethod(clientLogger, clientRootLogger);\n return {\n error: createLogger(clientRootLogger, \"error\"),\n warning: createLogger(clientRootLogger, \"warning\"),\n info: createLogger(clientRootLogger, \"info\"),\n verbose: createLogger(clientRootLogger, \"verbose\"),\n };\n }\n\n return {\n setLogLevel: contextSetLogLevel,\n getLogLevel: contextGetLogLevel,\n createClientLogger: contextCreateClientLogger,\n logger: clientLogger,\n };\n}\n\nconst context = createLoggerContext({\n logLevelEnvVarName: \"TYPESPEC_RUNTIME_LOG_LEVEL\",\n namespace: \"typeSpecRuntime\",\n});\n\n/**\n * Immediately enables logging at the specified log level. If no level is specified, logging is disabled.\n * @param level - The log level to enable for logging.\n * Options from most verbose to least verbose are:\n * - verbose\n * - info\n * - warning\n * - error\n */\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const TypeSpecRuntimeLogger: TypeSpecRuntimeClientLogger = context.logger;\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel): void {\n context.setLogLevel(logLevel);\n}\n\n/**\n * Retrieves the currently specified log level.\n */\nexport function getLogLevel(): TypeSpecRuntimeLogLevel | undefined {\n return context.getLogLevel();\n}\n\n/**\n * Creates a logger for use by the SDKs that inherits from `TypeSpecRuntimeLogger`.\n * @param namespace - The name of the SDK package.\n * @hidden\n */\nexport function createClientLogger(namespace: string): TypeSpecRuntimeLogger {\n return context.createClientLogger(namespace);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typespec/ts-http-runtime",
3
- "version": "0.2.3-alpha.20250529.3",
3
+ "version": "0.2.3-alpha.20250603.2",
4
4
  "description": "Isomorphic client library for making HTTP requests in node.js and browser.",
5
5
  "sdk-type": "client",
6
6
  "type": "module",
@@ -113,8 +113,8 @@
113
113
  "execute:samples": "echo skipped",
114
114
  "extract-api": "dev-tool run build-package && dev-tool run extract-api",
115
115
  "format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,cjs,mjs,json}\"",
116
- "lint": "eslint README.md package.json api-extractor.json src test",
117
- "lint:fix": "eslint README.md package.json api-extractor.json src test --fix --fix-type [problem,suggestion]",
116
+ "lint": "eslint package.json api-extractor.json src test",
117
+ "lint:fix": "eslint package.json api-extractor.json src test --fix --fix-type [problem,suggestion]",
118
118
  "pack": "npm pack 2>&1",
119
119
  "test": "npm run test:node && npm run test:browser",
120
120
  "test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --no-test-proxy --browser",