import cmdSerializer from "./cmd-serializer.js";
import configSerializer from "./config-serializer.js";
import { ProblemStream, getEnv, validateLogLevel, withSanitizer } from "./utils.js";
import errSerializer from "./err-serializer.js";
import { RenovateStream } from "./pretty-stdout.js";
import { RenovateLogger } from "./renovate-logger.js";
import fs from "fs-extra";
import { isNonEmptyStringAndNotWhitespace, isString } from "@sindresorhus/is";
import { randomUUID } from "node:crypto";
import * as bunyan$1 from "bunyan";
import upath from "upath";

//#region lib/logger/index.ts
const problems = new ProblemStream();
let stdoutLevel = validateLogLevel(getEnv("LOG_LEVEL"), "info");
function logLevel() {
	return stdoutLevel;
}
function createDefaultStreams(stdoutLevel, problems, logFile) {
	const stdout = {
		name: "stdout",
		level: stdoutLevel,
		stream: process.stdout
	};
	// v8 ignore else -- TODO: add test #40625
	if (getEnv("LOG_FORMAT") !== "json") {
		const prettyStdOut = new RenovateStream();
		prettyStdOut.pipe(process.stdout);
		stdout.stream = prettyStdOut;
		stdout.type = "raw";
	}
	return [
		stdout,
		{
			name: "problems",
			level: "warn",
			stream: problems,
			type: "raw"
		},
		isString(logFile) ? createLogFileStream(logFile) : void 0
	].filter(Boolean);
}
function createLogFileStream(logFile) {
	const directoryName = upath.dirname(logFile);
	fs.ensureDirSync(directoryName);
	const file = {
		name: "logfile",
		path: logFile,
		level: validateLogLevel(getEnv("LOG_FILE_LEVEL"), "debug")
	};
	const logFileFormat = getEnv("LOG_FILE_FORMAT");
	if (isNonEmptyStringAndNotWhitespace(logFileFormat) && logFileFormat === "pretty") file.type = "raw";
	return file;
}
function serializedSanitizedLogger(streams) {
	return bunyan$1.createLogger({
		name: "renovate",
		serializers: {
			body: configSerializer,
			cmd: cmdSerializer,
			config: configSerializer,
			migratedConfig: configSerializer,
			originalConfig: configSerializer,
			presetConfig: configSerializer,
			oldConfig: configSerializer,
			newConfig: configSerializer,
			err: errSerializer
		},
		streams: streams.map(withSanitizer)
	});
}
const bunyanLogger = serializedSanitizedLogger(createDefaultStreams(stdoutLevel, problems, getEnv("LOG_FILE")));
const loggerInternal = new RenovateLogger(bunyanLogger, getEnv("LOG_CONTEXT") ?? randomUUID(), {});
const logger = loggerInternal;
function setContext(value) {
	loggerInternal.logContext = value;
}
function getContext() {
	return loggerInternal.logContext;
}
function setMeta(obj) {
	loggerInternal.setMeta(obj);
}
function addMeta(obj) {
	loggerInternal.addMeta(obj);
}
function removeMeta(fields) {
	loggerInternal.removeMeta(fields);
}
function withMeta(obj, cb) {
	addMeta(obj);
	try {
		return cb();
	} finally {
		removeMeta(Object.keys(obj));
	}
}
function addStream(stream) {
	loggerInternal.addStream(stream);
}
/**
* For testing purposes only
* @param name stream name
* @param level log level
* @private
*/
function levels(name, level) {
	bunyanLogger.levels(name, level);
	// v8 ignore else -- TODO: add test #40625
	if (name === "stdout") stdoutLevel = level;
}
function getProblems() {
	return problems.getProblems();
}
function clearProblems() {
	return problems.clearProblems();
}

//#endregion
export { addMeta, addStream, clearProblems, createDefaultStreams, getContext, getProblems, levels, logLevel, logger, removeMeta, setContext, setMeta, withMeta };
//# sourceMappingURL=index.js.map