Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
688 views
in Online Assessments by Expert (46,090 points)
edited by | 688 views

1 Answer

0 like 0 dislike
Best answer

Flipkart Online Assessment

by Expert (46,090 points)
0 0
Enum Level:
------Info
------Warn
------Error
------Fatal

ISink
------writemessage(Level, namespace, message)

FileSink : ISink
------------File
------------FileSink(path)
------------------openfile()
------------writemessage(Level , Namespace , message)
------------------writetofile()

ConsoleSink : ISink
------------Console
------------ConsoleSink(addr)
------------------openconsole()
------------writemessage(Level , Namespace , message)
------------------writetoconsole()

DatabaseSink : ISink
------------DB
------------DatabaseSink(session)
------------------opendatabase()
------------writemessage(Level , Namespace , message)
------------------writetodb()

LoggerConfig
------------Hash<Level,ISink> mapping
------------LogLevel {get;set;}
------------mapLevelToSink(Level ,ISink)
------------------mapping(level,sink)
------------getSink(Level)
------------------mapping[lebel]

Logger
------LoggerConfig config
------Logger(LoggerConfig)
------------config
------Log(level, namespace, msg)
------------if(level > config.loglevel
------------while(level < Level.iterate)
------------------config.get(level).writemessage(level,namespace,msg)
------------level++
0 0
You only need to come up with a logger. Do not worry about sink and client application just yet.

Have an enum for LogLevels i.e. FATAL, ERROR, WARN, INFO

Have a main Logger class. Provide API in your logger class getMessage(String message, LogLeveLEnum level) which client can use to send log message and associated log level.

Have a field to store configured log level in your Logger class. Provide API and maybe have constructor of logger class set a log level and store it in a Enum field, something like setLogLevel(LogLevelEnum level)

After receiving a message via getMessage forward it to another function to process it. (filter it if it is less than configured log level, otherwise append timestamp, loglevel)

We can use command pattern to tie a sink to a loglevel and a message can be passed on to appropriate command based on the log level. (Logger class can probably keep a Map of logLevel as key, and various classes implementing command interface to map a logLevel to a command. This map should expose setter APIs to change behavior at run-time.)

Note: Based on example config at the bottom it looks like we can have different time-formats too for different logLevels in which case we can delegate the responsibility to append timestamp to command itself by having a timeformat interface field in it and use strategy pattern to set various concrete timeStamp implementations