github-code-scanning[bot] commented on code in PR #7673:
URL: https://github.com/apache/trafficcontrol/pull/7673#discussion_r1273967515


##########
experimental/traffic-portal/src/app/utils/logging.ts:
##########
@@ -0,0 +1,227 @@
+/**
+ * @license Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * LogStreams are the underlying raw event writers used by {@link Logger}s. The
+ * simplest and most useful example of a LogStreams implementation is 
`console`.
+ */
+export interface LogStreams {
+       debug(...args: unknown[]): void;
+       info(...args: unknown[]): void;
+       error(...args: unknown[]): void;
+       warn(...args: unknown[]): void;
+}
+
+/**
+ * A LogLevel describes the verbosity of logging. Each level is cumulative,
+ * meaning that a logger set to some level will also log all of the levels 
above
+ * it.
+ */
+export const enum LogLevel {
+       /** Log only errors. */
+       ERROR,
+       /** Log warnings and errors. */
+       WARN,
+       /** Log informational messages, warnings, and errors. */
+       INFO,
+       /** Log debugging messages, informational messages, warnings, and 
errors. */
+       DEBUG,
+}
+
+/**
+ * Converts a log level to a human-readable string.
+ *
+ * @example
+ * console.log(logLevelToString(LogLevel.DEBUG));
+ * // Output:
+ * // DEBUG
+ *
+ * @param level The level to convert.
+ * @returns A string representation of `level`.
+ */
+export function logLevelToString(level: LogLevel): string {
+       switch(level) {
+               case LogLevel.DEBUG:
+                       return "DEBUG";
+               case LogLevel.ERROR:
+                       return "ERROR";
+               case LogLevel.INFO:
+                       return "INFO";
+               case LogLevel.WARN:
+                       return "WARN";
+       }
+}
+
+/**
+ * A Logger logs things. The output streams are customizable, mostly for 
testing
+ * but also in case we want to write directly to a file handle someday.
+ *
+ * The output format is a bit customizable, it allows for messages to be
+ * prefixed in a number of ways:
+ * - With the level at which the message was logged
+ * - With a timestamp for the time at which logging occurred (ISO format)
+ * - With some static string
+ *
+ * in that order. For example, if all of them are specified:
+ *
+ * @example
+ * (new Logger(console, LogLevel.DEBUG, "test", true, true)).info("quest");
+ * // Output (example date is UNIX epoch):
+ * // INFO 1970-01-01T00:00:00.000Z test: quest
+ */
+export class Logger {
+       private readonly prefix: string;
+
+       /**
+        * Constructor.
+        *
+        * @param streams The output stream abstractions.
+        * @param level The level at which the logger operates. Any level higher
+        * than the one specified will not be logged.
+        * @param prefix If given, prepends a prefix to each message.
+        * @param useLevelPrefixes If true, log lines will be prefixed with the 
name
+        * of the level at which they were logged (useful if all streams point 
to
+        * the same file descriptor).
+        * @param timestamps If true, each log line will be accompanied by a
+        * timestamp prefix (note that the time is determined when logging 
occurs,
+        * not necessarily when the logging method is called).
+        */
+       constructor(
+               private readonly streams: LogStreams,
+               level: LogLevel,
+               prefix: string = "",
+               private readonly useLevelPrefixes: boolean = true,
+               private readonly timestamps: boolean = true,
+       ) {
+               if (prefix) {
+                       prefix = prefix.trim().replace(/:+$/, "").trimEnd();

Review Comment:
   ## Polynomial regular expression used on uncontrolled data
   
   This [regular expression](1) that depends on [a user-provided value](2) may 
run slow on strings with many repetitions of ':'.
   
   [Show more 
details](https://github.com/apache/trafficcontrol/security/code-scanning/294)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to