Logging in Java with slf4j文档格式.docx

上传人:b****2 文档编号:4192920 上传时间:2023-05-02 格式:DOCX 页数:14 大小:73.81KB
下载 相关 举报
Logging in Java with slf4j文档格式.docx_第1页
第1页 / 共14页
Logging in Java with slf4j文档格式.docx_第2页
第2页 / 共14页
Logging in Java with slf4j文档格式.docx_第3页
第3页 / 共14页
Logging in Java with slf4j文档格式.docx_第4页
第4页 / 共14页
Logging in Java with slf4j文档格式.docx_第5页
第5页 / 共14页
Logging in Java with slf4j文档格式.docx_第6页
第6页 / 共14页
Logging in Java with slf4j文档格式.docx_第7页
第7页 / 共14页
Logging in Java with slf4j文档格式.docx_第8页
第8页 / 共14页
Logging in Java with slf4j文档格式.docx_第9页
第9页 / 共14页
Logging in Java with slf4j文档格式.docx_第10页
第10页 / 共14页
Logging in Java with slf4j文档格式.docx_第11页
第11页 / 共14页
Logging in Java with slf4j文档格式.docx_第12页
第12页 / 共14页
Logging in Java with slf4j文档格式.docx_第13页
第13页 / 共14页
Logging in Java with slf4j文档格式.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Logging in Java with slf4j文档格式.docx

《Logging in Java with slf4j文档格式.docx》由会员分享,可在线阅读,更多相关《Logging in Java with slf4j文档格式.docx(14页珍藏版)》请在冰点文库上搜索。

Logging in Java with slf4j文档格式.docx

K2,k2tog,kacrosstolast4sts,ssk,k2.RepDecRoweveryotherrow10(12,13,14)timesmore--38(40,46,48)sts.Continueeventoapprox211/2(22,221/2,23)inchesfrombegendingwithaWSrow."

means-mostgrandmothersknowthat-andtellheryouwritethosebutforcomputers. 

She'

llnodandunderstandcompletely. 

IfyouarelikemostlearningprogrammersyouhaveNOideawhatthequoteabovemeansatall. 

Well,itispartofaknittingrecipeforasweater[ 

]andthatiswhatprogrammingisbasicallyallabout-writingknittingrecipesforcomputers. 

Programmingisacraftittakesyearstomaster

PeterNorvigwritesinhisonlinearticle 

TeachYourselfProgramminginTenYears 

-arecommendedread-thatittakes10yearsorperhaps10000hoursofdeliberatepracticetodevelopexpertiseinanarea. 

Forprogrammersthatmeanswritingprogramsandreadingprograms,butunfortunatelythereisnotyetageneraltraditionofexperiencedprogrammersmentoringnewprogrammersinperson,soyouwillhavetolearnalotonyourown. 

Byusingthetechniquesdescribedinthisdocumentyouwillhaveagreaterchancetoavoidsomeveryembarrassingandexhaustingexperiencesinyourfuturejobs,andhopefullynothavingtounlearnahabitortwo. 

Ifyouneedtogetexperiencewhilestudyingorpreparingtogetajob,IwouldrecommendthatyoulocateanOpenSourceprojectwhichyouaregenuinelyinterestedin-forinstanceifyouneeditinyourownprojects-andhelpthem. 

Thiscanbothbeprogramming,butalsowritingdocumentation-asgooddocumentationrequiresthoroughunderstandingofthesoftwarebeingdocumented. 

Thisalsogivesyouexperienceworkingwithateamwhichcommunicateelectronicallyandusetoolsthatallowmultipledeveloperstoworkonthesameprojectwithoutdisturbingeachothersfiles. 

BychoosinganOpenSourceprojectyoucandemonstratewhatyouhavedonetoafutureemployeraspartoftheinterviewiftheyaskwhatexperienceyouhave. 

Andyoutryandtelltheyoungpeopleoftodaythat.....theywon'

tbelieveyou."

JavawasdesignedbySunMicrosystems,whichhappenedtomakealivingatthetimesellingcomputersrunningtheSundialectoftheUnixoperatingsystem. 

ItisnotasurprisethenthatJavaprogramstendtolooklikeotherUnixprograms,whichweredesignedtoworkonthecomputeryouseeinthepicturebelow. 

PictureshowsKenThompsonandDennisRitchieworkingatthefirstrealUnixmachine. 

Pleasenotethatthereisrealpaperinthe"

typewriters"

theclosetbehindthemenis 

the 

computer,andthatthecircularcontainersontopofthecomputercontainprogramanddataonpapertape. 

Thingsweredifferentin1973.

UnderUnixeveryprogramhasthreeinput/outputconnectionsavailable-namely 

∙standardinput 

-thisprovidesthethingstheusertypetotheprogram. 

(Java:

System.in)

∙standardoutput 

-thisisnormalinformation(Java:

System.out)

∙standarderror 

-thisisinformationabouterrorsituations(Java:

System.err)

TheUnixphilosphywasstatedbyMcIlroy[ 

]tobe"

Writeprogramsthatdoonethinganddoitwell.Writeprogramstoworktogether.Writeprogramstohandletextstreams,becausethatisauniversalinterface. 

.Thismeansyoucanlettheoutputofoneprogrambetheinputofanotherprogram-calledapipe 

-encouragingsmallspecializedprogramsbeingcombinedtocompletealargertask,likecreatingalistofwordsinafilebyfirstextractingallwords,sortingthem,andtheneliminateduplicates. 

UnfortunatelythishasnotcaughtoninJava,mostlybecausetheJavaenvironmenttakestoolongtostartforsmallprogramstobereallyusable. 

Theconceptsarestillthesame,however. 

SothesimplestpossibleJavaprogramusesSystem.outtoprintouttheline"

HelloWorld"

:

1:

publicclassHelloWorld{

2:

publicstaticvoidmain(String[]args){

3:

System.out.println("

);

4:

}

5:

Thisshouldbenamed"

HelloWorld.java"

andiscompilablefromthecommandlinewith"

javacHelloWorld.java"

andthecompiledclasscanthenbeinvokedwith"

javaHelloWorld"

Line1declaresthistobetheclassnamedHelloWorld-thereisno"

package"

linesoitisjustHelloWorldinsteadofe.g.com.bigcoorporation.HelloWorld. 

Line2isthemagicwordsneededtobeabletoinvoketheclassfilefromthecommandlineas"

Line3printstheline"

tostandardoutput. 

Now,whydid 

that 

happen?

Oneoftheunfortunatesideeffectsofprogrammingforalivingisthatpeopleareusingyourprograms,andtheytendtofindthatyourprogramsdonotworkas 

they 

expect-thisistraditionallycalleda"

bug"

]-anditisthenyourresponsibilitytoidentifyandcorrecttheproblem. 

Inmostcases,allyouhavetoworkwithistheuserdescribingwhathappenedandwhatwasexpected. 

Theprogramhasendedandisforallpracticalpurposesunavailabletoyou. 

Henceawayofsavinginformationfromwithintheprogramforlaterisneeded,muchinthesamewaythatlogbooksareusedonshipstoaidinnavigation. 

Theterm"

logging"

isusedingeneralfortheprocessofsystematicallysavinginformationforlater. 

SeveralloggingsolutionsareavailabletoJavaprogrammers. 

Allofthesegenerallysharetheabilitytolog:

∙Theareaofthecodecontainingthelogstatement.

∙Thetimewhenthelogstatementwasinvoked.

∙Animportancelevelofthislogstatement.

∙Anexception(withaccompanyingstacktrace).

∙Alogmessage.

Thishasproventobeveryhelpfulforlogsintendedforthehumanreader. 

Facilitiesavailableinsomeloggingframeworksinclude:

∙storingeventsinadatabase

∙alertusersbyemail,instantmessagingoranyotherelectronicmeans

(butthatistooadvancedtobecoveredinthisintroduction-itisjusttogiveyouanideaofwhatispossible)

TheHelloWorldprogramrewrittentousetheSLF4Jloggingsolution(fullinstructionsareinappendixA)andputinthe"

demo"

package,lookslike:

==== 

packagedemo;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

publicclassHelloWorld{

Loggerlog=LoggerFactory.getLogger(HelloWorld.class);

log.info("

}

===== 

andgeneratesthefollowingoutput:

====

2009-06-0710:

38:

21demo.HelloWorldmain

INFO:

HelloWorld

Thefirstlinecontainsadateandtimestamp,thenameofthelogger-here"

demo.HelloWorld"

-andthenameofthemethodcontainingthelog.infostatement-here"

main"

Thesecondlinecontainstheimportancelevel-here"

INFO"

-andthemessagepassed 

-here"

Notehowmuchofthisinformationwasderivedautomatically!

Ifweaddamethodcallwithawait,wecanseethatthetimestampchanges,andthatthemethodnamesinthelogcorrectlyindicatesifthelogstatementisdonefrom"

or"

waitTwoSeconds"

publicclassHelloWorld2{

publicstaticvoidmain(String[]args)throwsException{

Loggerlog=LoggerFactory.getLogger(HelloWorld2.class);

waitTwoSeconds(log);

Done"

privatestaticvoidwaitTwoSeconds(Loggerlog)throwsException{

Thread.sleep(2000);

Slept2seconds"

=====

withthefollowingoutput:

53:

52demo.HelloWorld2main

54demo.HelloWorld2waitTwoSeconds

Slept2seconds

54demo.HelloWorld2main

Done

Ifyouwereshownthislog,youwouldbeabletodeterminethattheprogramhadrunasexpectedtothestatementlogging"

inthemainmethodofdemo.HelloWorld2andyoushouldnotlookforproblemshappeningbeforethatstatement. 

Logginginformationstoredinvariables

Ifyouneedtoaddinformationstoredinavariable,slf4jallowyoutospecifythiswith{}.(Note:

ExampleworkswithJava5andlater) 

publicclassZeroToFour{

Loggerlog=LoggerFactory.getLogger(ZeroToFour.class);

for(inti=0;

i<

5;

i++){

i={},i*i={}"

i,i*i);

whichoutputs 

2009-06-0713:

23:

57demo.ZeroToFourmain

i=0,i*i=0

i=1,i*i=1

i=2,i*i=4

i=3,i*i=9

i=4,i*i=16

Allobjectscanbeloggedinthisway,andthetoString()methodiscalledtogetthestringtolog.Notethatint,long,double,floats,charsarenotobjects,butthatinJava5andlatertheyareinvisiblyconvertedcorrectly. 

Loggingproblems

Allprogramsregardlessofprogramminglanguagemus

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2