Clean Code



Download 3,58 Mb.
Pdf ko'rish
bet124/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   120   121   122   123   124   125   126   127   ...   384
Bog'liq
Clean Code

Learning 
log4j
This time we find that the 
Appender
has no output stream. Odd—it seems logical that it’d
have one. After a little help from Google, we try the following:
@Test
public void testLogAddAppender() {
Logger logger = Logger.getLogger("MyLogger");
logger.removeAllAppenders();
logger.addAppender(new ConsoleAppender(
new PatternLayout("%p %t %m%n"), 
ConsoleAppender.SYSTEM_OUT));
logger.info("hello");
}
That worked; a log message that includes “hello” came out on the console! It seems odd
that we have to tell the 
ConsoleAppender
that it writes to the console. 
Interestingly enough, when we remove the 
ConsoleAppender.SystemOut
argument, we
see that “hello” is still printed. But when we take out the 
PatternLayout
, it once again com-
plains about the lack of an output stream. This is very strange behavior. 
Looking a little more carefully at the documentation, we see that the default
ConsoleAppender
constructor is “unconfigured,” which does not seem too obvious or useful.
This feels like a bug, or at least an inconsistency, in 
log4j
.
A bit more googling, reading, and testing, and we eventually wind up with Listing 8-1.
We’ve discovered a great deal about the way that 
log4j
works, and we’ve encoded that
knowledge into a set of simple unit tests.
Listing 8-1 
LogTest.java
public class LogTest {
private Logger logger;
@Before
public void initialize() {
logger = Logger.getLogger("logger");
logger.removeAllAppenders();
Logger.getRootLogger().removeAllAppenders();
}
@Test
public void basicLogger() {
BasicConfigurator.configure();
logger.info("basicLogger");
}
@Test
public void addAppenderWithStream() {
logger.addAppender(new ConsoleAppender(
new PatternLayout("%p %t %m%n"),
ConsoleAppender.SYSTEM_OUT));
logger.info("addAppenderWithStream");
}


118

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   120   121   122   123   124   125   126   127   ...   384




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish