import { getEnv } from "./utils.js";
import { withSanitizer } from "./with-sanitizer.js";
import errSerializer from "./err-serializer.js";
import cmdSerializer from "./cmd-serializer.js";
import configSerializer from "./config-serializer.js";
import { RenovateStream } from "./pretty-stdout.js";
import { createRequire } from "node:module";
import fs from "fs-extra";
import { isNonEmptyStringAndNotWhitespace, isString, isUndefined } from "@sindresorhus/is";
import upath from "upath";
//#region lib/logger/bunyan.ts
const bunyan = createRequire(import.meta.url)("bunyan");
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") {
		stdout.stream = new RenovateStream(process.stdout);
		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.stream = new RenovateStream(fs.createWriteStream(logFile, {
			flags: "a",
			encoding: "utf8"
		}), false);
		file.type = "raw";
		delete file.path;
	}
	return file;
}
function serializedSanitizedLogger(streams) {
	return bunyan.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)
	});
}
function createLogger(stdoutLevel, problems) {
	return serializedSanitizedLogger(createDefaultStreams(stdoutLevel, problems, getEnv("LOG_FILE")));
}
/**
* A function that terminates execution if the log level that was entered is
*  not a valid value for the Bunyan logger.
* @param logLevelToCheck
* @returns returns the logLevel when the logLevelToCheck is valid or the defaultLevel passed as argument when it is undefined. Else it stops execution.
*/
function validateLogLevel(logLevelToCheck, defaultLevel) {
	if (isUndefined(logLevelToCheck) || isString(logLevelToCheck) && [
		"trace",
		"debug",
		"info",
		"warn",
		"error",
		"fatal"
	].includes(logLevelToCheck)) return logLevelToCheck ?? defaultLevel;
	bunyan.createLogger({
		name: "renovate",
		streams: [{
			level: "fatal",
			stream: process.stdout
		}]
	}).fatal({ logLevel: logLevelToCheck }, "Invalid log level");
	process.exit(1);
}
//#endregion
export { createLogger, validateLogLevel };

//# sourceMappingURL=bunyan.js.map