hslogger-1.3.1.0/0000755000000000000000000000000007346545000011655 5ustar0000000000000000hslogger-1.3.1.0/CHANGELOG.md0000755000000000000000000000107007346545000013467 0ustar0000000000000000See also https://pvp.haskell.org/faq #### 1.3.1.0 *(minor)* - Evaluate message before taking lock in simple handler ([#49](https://github.com/haskell-hvr/hslogger/pull/49)) - Define `Typeable`, `Data`, `Generic` and `NFData` instances for `System.Log.Priority` ([#43](https://github.com/haskell-hvr/hslogger/pull/43)) ## 1.3.0.0 *(major)* - **[semantic change]** Messages are encoded as UTF-8 (previously the encoding was locale dependent) for the Syslog and Growl backends - Add support for `network-3.0`; remove redundant dependency on `directory` and `process` hslogger-1.3.1.0/LICENSE0000644000000000000000000000272007346545000012663 0ustar0000000000000000Copyright (c) 2004 - 2011 John Goerzen All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of John Goerzen nor the names of the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. hslogger-1.3.1.0/LICENSE0000755000000000000000000000272007346545000012666 0ustar0000000000000000Copyright (c) 2004 - 2011 John Goerzen All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of John Goerzen nor the names of the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. hslogger-1.3.1.0/Setup.hs0000644000000000000000000000011207346545000013303 0ustar0000000000000000#!/usr/bin/env runhaskell import Distribution.Simple main = defaultMain hslogger-1.3.1.0/contrib/java/0000755000000000000000000000000007346545000014236 5ustar0000000000000000hslogger-1.3.1.0/contrib/java/build.xml0000755000000000000000000000227507346545000016070 0ustar0000000000000000 simple example build file hslogger-1.3.1.0/contrib/java/hslogger4j-plugins.xml0000755000000000000000000000123007346545000020506 0ustar0000000000000000 hslogger-1.3.1.0/contrib/java/hslogger4j.jar0000755000000000000000000002165107346545000017014 0ustar0000000000000000PK jQ7 META-INF/PK iQ7M^jMETA-INF/MANIFEST.MFMLK-. K-*ϳR03rCq,HLHU%LyRKRSt*AM u-4 rRs JKRt<4yxPK jQ7org/PK jQ7 org/haskell/PK jQ7org/haskell/hslogger4j/PK jQ7ЎY*org/haskell/hslogger4j/HsloggerLevel.classOGƿmib%mlքkZh8d$mVdeS_"/UTCB FK"只zx4Xi7of\v^?c7q,1<;V8`raC!BaMC18lEM-uL1cNǼ e?ۗb Ұ-p)VήY&fˮʶvB켏,se  -du=YhW)HDjt,)).[^%$pPoT2v㺙[Ta殪ǎ(^v$3TVN2΁ԾTU[7f~lg\VlZeLGO %^(E00;^;p2\xie]wlVln d'vf fg񎡗`K{ZG"Y>"(y f8r0a"Χm4y>ѷG3Dx1TDpHAZ f|aY`qӼ Z!,? \a#s6㶓fu4h5LF:ѸgD")i KضmX2 q-kL5LBzY;ଆGЭa6k؂nmP֣ǒq _S2 !v 3-=ְnYI'"ĺ5Mm< = 4| >8zNOeeJ!usFtLQ SHYnSw݃[vN"\O/8#_j _kjK\ =1̹X!bOq~ iAbˆgGL[#M~ؼDzN H)L+y(zJg '-9cV/(>16LR2;`–%; U^[AlRlj#L^7).j=pHeQSG=%YSMIyN" vjWu=rVT?™&XܧnrJ z1<ʦS#j!mUr\9u~]~<ȞOr:j!J0g S,9?.b˜MTƒZѬES)Hl-0%K'^ijzQGbDͽu*R8tj¶7d)]W7$ְzK%1QӄIF;hC-J.-3cԶ_!0"' 011qQDn󶽀E(}:t&vy0 kEE(N y7q*"D0 ?E4MѮ%pk "剎?B w(6֔(Q40 `iƱZwkYEtbU"8 G/!6X8Y^[s"k:As` @L`x&^}tvZkiy e PK jQ7IC/org/haskell/hslogger4j/LogFileXMLReceiver.classWyxW-yei(4DbiӴqZZ~7YkuB.P9ԡ㸔rPr7K,GkS{373=؋ц4w.>|x%^!:kCx^Fo "{D9o a xwww{E/?LWP#89 cx`BăA4f9.0|X_d#|/~FgE|N87% :-O Uf}d3mֱɜ CѲy hHiz&!KZfN|LwKk#L/SVT X')gӄ}U9Sl9Bҙl!*ZZ!^x2qꖎA5횅ijy10dsk:˚'8&ARyD@eiɤ`EB4Jd"s5ZeAVnhGCRq⨵n8'3JvEC2옠Hq+qY!ѓ~9N2崩L&ΙdĠt-M&icdc2\GزM-JTD,%-ňbe;_ugkkld SĘAgNΓ;X;מْ]-SvyB;Z:p%3JXģF$܄Kq GqD xW'R|8̇nN;jCber\m8іЎc" 8%1 _jŇA—9+~>+d6vz"qsM_ǣyEr;oH&$|p썮#GLg#vՐz#Tt>6"~ mJqDkT5-,ky]?K)~&q ?/DR¯k I ~K%tɧdꂞϙlIk9`a"(OgEY9$<6w\NUҲ_8^sj %oadR!R@[W~ 6Am5~Xbr ]K˚.9iKg0])GU3M-9ǖ)]rl&5UhS~ ؼRHd 3u:ç\[%aq3)_;xt JeJٷ=ޔ\x(Q7 %az@ݍȮ#o5(C7Tr#{_P:2i֪'鬷b*FM5kMwf]1<%dP>'U#ZP_Yc9r EŢ7:baQ*?ZDt+pv NvYzIҬ֩@ C(fU΢f%}\{ ;Ugt<3mꢫ>!v9},CV۳6?_D=Z:NXWs>b5Ѩ,пklV*alkGlND)i)үD{0] 8_9ll)`kmD hÓ:p!24r[ElϡsC.\oc;Dv=﫸H\YﻀVKL 3A-|KKM%Mu ~0@ 0Gt (|3j:̃)5XBKsd->du7GKh)@K1v6^9}Yΐ +]+b#k.Cv4O";~xuU|>eOx]gjlFNZ>e@G*rvZVEe`Tj Nt[zh#5^XtJ#Ut2u GLq܈}l6k> |/5FxTΦ("vUtvVNZiʕ!c7kx?>fnw=PLVj^qjurVnI͜Bm8!ZWV'L2m+}it#m%lYݙDΐMLiɱ)MɂߚWVvB|8y p ~g-767N|xۚձ@&ת)`C}M.xKI6ꘇR&.1&Siwq[ćwit;%8v\ xfd΀5_ڜ 2x|ue<,FctJNK+CgP!iaBuQu { QT ?Ҩk n+Ш K縚pjthe8.Dž3t ;3E4:N*N} ݯdh4,lGCbhRNk&vX7*A(3BL錱WσbO>cƨQl N:K>Yͭ:.FҭE1ΰ3i- z$GAR(tF*G5:K!zBoF&Yxaz2du 6mA4z'k(S]4z7q{/O4 g=} )-9X4'sʍI}8"VcAn"i3^˱.}ƁSŖ(JW@Br}!1MԈhˏu,ᷛ[G ?z\0GCfX3oyr˟+CmX.sºy4s]윴o1i,e`nKs`"Z鈢=2l$% -ix{VD+ cB%ʬaނ+Cɬ2Z=>;c wT`VЪٰuߖ)uk) Yzג$q 2/{F3V4$+rFnp~tq(%9?IS^~nG?4 rFWRS3Mx!K;9͎;nS+S ^QS$, `Kc:h~i:Ƚw0DDDiv >\KHnWY]]auJBcyq.. 8a<;J]e./ewiw/ wnk |ǏUĜ7!"cymy"[ƍR4ށ MLvs?%f['viܚ)e;Ш o+4czc{Z ]㶣S QT)$qw&j>Ӄ_%n56k:k柟Im!N2nze;w;,o ㎪ X+8}w2:eI;`+汻u.`O[%8_>wǻ/&cm$p(&>L 7T8GX+yL% B<#=x,-PBD5P)F%XJT!L_~^-חnB"V3noy(+3,S>;0|`0{/PJtHV&K\doWlAWaph{)JNu\qmQoIc'~F8sC:<b`momӹ蛸Eh{h+^gqp]Ax{_~6],{RޫXZX*P&{v~U..k Ճ_ ~]Y^Z!PK jQ7 AMETA-INF/PK iQ7M^j+META-INF/MANIFEST.MFPK jQ7Aorg/PK jQ7 Aorg/haskell/PK jQ7Aorg/haskell/hslogger4j/PK jQ7ЎY*<org/haskell/hslogger4j/HsloggerLevel.classPK jQ7~:( 1.org/haskell/hslogger4j/LogFileXMLReceiver$1.classPK jQ7IC/ org/haskell/hslogger4j/LogFileXMLReceiver.classPK jQ7f 'org/haskell/hslogger4j/XMLDecoder.classPK  hslogger-1.3.1.0/contrib/java/org/haskell/hslogger/0000755000000000000000000000000007346545000020262 5ustar0000000000000000hslogger-1.3.1.0/contrib/java/org/haskell/hslogger/HsloggerLevel.java0000755000000000000000000000772407346545000023704 0ustar0000000000000000/* * Copyright 1999,2004 The Apache Software Foundation. * * 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. */ package org.haskell.hslogger4j; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Level; /** * An extension of the Level class that provides support for java.util.logging * Levels. * * @author Scott Deboy * */ public class HsloggerLevel extends org.apache.log4j.Level { public final static int EMERGENCY_INT = 51000; // FATAL public final static int ALERT_INT = 50000; public final static int CRITICAL_INT = 41000; public final static int ERROR_INT = 40000; // ERROR public final static int WARNING_INT = 30000; // WARN public final static int NOTICE_INT = 21000; public static final int INFO_INT = 20000; // INFO public static final int DEBUG_INT = 10000; // DEBUG public static final HsloggerLevel EMERGENCY = new HsloggerLevel(EMERGENCY_INT, "EMERGENCY", 0); public static final HsloggerLevel ALERT = new HsloggerLevel(ALERT_INT , "ALERT" , 1); public static final HsloggerLevel CRITICAL = new HsloggerLevel(CRITICAL_INT , "CRITICAL" , 2); public static final HsloggerLevel ERROR = new HsloggerLevel(ERROR_INT , "ERROR" , 3); public static final HsloggerLevel WARNING = new HsloggerLevel(WARNING_INT , "WARNING" , 4); public static final HsloggerLevel NOTICE = new HsloggerLevel(NOTICE_INT , "NOTICE" , 5); public static final HsloggerLevel INFO = new HsloggerLevel(INFO_INT , "INFO" , 6); public static final HsloggerLevel DEBUG = new HsloggerLevel(DEBUG_INT , "DEBUG" , 7); protected HsloggerLevel(int level, String levelStr, int syslogEquivalent) { super(level, levelStr, syslogEquivalent); } /** Convert an integer passed as argument to a level. If the conversion fails, then this method returns the specified default. */ public static HsloggerLevel toLevel(int val, HsloggerLevel defaultLevel) { switch (val) { case EMERGENCY_INT: return EMERGENCY; case ALERT_INT: return ALERT; case CRITICAL_INT: return CRITICAL; case ERROR_INT: return ERROR; case WARNING_INT: return WARNING; case NOTICE_INT: return NOTICE; case INFO_INT: return INFO; case DEBUG_INT: return DEBUG; default: return defaultLevel; } } public static Level toLevel(int val) { return toLevel(val, CRITICAL); } public static List getAllPossibleLevels() { ArrayList list=new ArrayList(); list.add(DEBUG); list.add(INFO); list.add(NOTICE); list.add(WARNING); list.add(ERROR); list.add(CRITICAL); list.add(ALERT); list.add(EMERGENCY); return list; } public static Level toLevel(String s) { return toLevel(s, Level.DEBUG); } public static Level toLevel(String sArg, Level defaultLevel) { if (sArg == null) { return defaultLevel; } String s = sArg.toUpperCase(); if (s.equals("EMERGENCY")) { return EMERGENCY; } if (s.equals("ALERT")) { return ALERT; } if (s.equals("CRITICAL")) { return CRITICAL; } if (s.equals("ERROR")) { return ERROR; } if (s.equals("WARNING")) { return WARNING; } if (s.equals("NOTICE")) { return NOTICE; } if (s.equals("INFO")) { return INFO; } if (s.equals("DEBUG")) { return DEBUG; } return defaultLevel; } } hslogger-1.3.1.0/contrib/java/org/haskell/hslogger/LogFileXMLReceiver.java0000755000000000000000000002340007346545000024516 0ustar0000000000000000/* * Copyright 1999,2004 The Apache Software Foundation. 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. */ package org.haskell.hslogger4j; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.util.Collection; import java.util.Iterator; import org.apache.log4j.xml.*; import org.apache.log4j.helpers.Constants; import org.apache.log4j.plugins.Receiver; import org.apache.log4j.rule.ExpressionRule; import org.apache.log4j.rule.Rule; import org.apache.log4j.spi.Decoder; import org.apache.log4j.spi.LoggingEvent; /** * LogFileXMLReceiver will read an xml-formated log file and make the events in the log file * available to the log4j framework. *

* This receiver supports log files created using log4j's XMLLayout, as well as java.util.logging * XMLFormatter (via the org.apache.log4j.spi.Decoder interface). *

* By default, log4j's XMLLayout is supported (no need to specify a decoder in that case). *

* To configure this receiver to support java.util.logging's XMLFormatter, specify a 'decoder' param * of org.apache.log4j.xml.UtilLoggingXMLDecoder. *

* Tailing -may- work, but not in all cases (try using a file:// URL). If a process has a log file * open, the receiver may be able to read and tail the file. If the process closes the file and * reopens the file, the receiver may not be able to continue tailing the file. *

* An expressionFilter may be specified. Only events passing the expression will be forwarded to the * log4j framework. *

* Once the event has been "posted", it will be handled by the appenders currently configured in the * LoggerRespository. * * @author Scott Deboy * @since 1.3 */ public class LogFileXMLReceiver extends Receiver { private String fileURL; private Rule expressionRule; private String filterExpression; private String decoder = "org.apache.log4j.xml.XMLDecoder"; private boolean tailing = false; private Decoder decoderInstance; private Reader reader; private static final String FILE_KEY = "file"; private String host; private String path; private boolean useCurrentThread; /** * Accessor * * @return file URL */ public String getFileURL() { return fileURL; } /** * Specify the URL of the XML-formatted file to process. * * @param fileURL */ public void setFileURL(String fileURL) { this.fileURL = fileURL; } /** * Accessor * * @return */ public String getDecoder() { return decoder; } /** * Specify the class name implementing org.apache.log4j.spi.Decoder that can process the file. * * @param _decoder */ public void setDecoder(String _decoder) { decoder = _decoder; } /** * Accessor * * @return filter expression */ public String getFilterExpression() { return filterExpression; } /** * Accessor * * @return tailing flag */ public boolean isTailing() { return tailing; } /** * Set the 'tailing' flag - may only work on file:// URLs and may stop tailing if the writing * process closes the file and reopens. * * @param tailing */ public void setTailing(boolean tailing) { this.tailing = tailing; } /** * Set the filter expression that will cause only events which pass the filter to be forwarded * to the log4j framework. * * @param filterExpression */ public void setFilterExpression(String filterExpression) { this.filterExpression = filterExpression; } private boolean passesExpression(LoggingEvent event) { if (event != null) { if (expressionRule != null) { return (expressionRule.evaluate(event)); } } return true; } public static void main(String[] args) { /* * LogFileXMLReceiver test = new LogFileXMLReceiver(); * test.setFileURL("file:///c:/samplelog.xml"); test.setFilterExpression("level >= TRACE"); * test.activateOptions(); */ } /** * Close the receiver, release any resources that are accessing the file. */ public void shutdown() { try { if (reader != null) { reader.close(); reader = null; } } catch (IOException ioe) { ioe.printStackTrace(); } } /** * Process the file */ public void activateOptions() { Runnable runnable = new Runnable() { public void run() { try { URL url = new URL(fileURL); host = url.getHost(); if (host != null && host.equals("")) { host = FILE_KEY; } path = url.getPath(); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { if (filterExpression != null) { expressionRule = ExpressionRule.getRule(filterExpression); } } catch (Exception e) { getLogger().warn("Invalid filter expression: " + filterExpression, e); } Class c; try { c = Class.forName(decoder); Object o = c.newInstance(); if (o instanceof Decoder) { decoderInstance = (Decoder) o; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { reader = new InputStreamReader(new URL(getFileURL()).openStream()); process(reader); } catch (FileNotFoundException fnfe) { getLogger().info("file not available"); } catch (IOException ioe) { getLogger().warn("unable to load file", ioe); return; } } }; if (useCurrentThread) { runnable.run(); } else { Thread thread = new Thread(runnable, "LogFileXMLReceiver-" + getName()); thread.start(); } } private void process(Reader unbufferedReader) throws IOException { BufferedReader bufferedReader = new BufferedReader(unbufferedReader); char[] content = new char[10000]; getLogger().debug("processing starting: " + fileURL); int length = 0; do { System.out.println("in do loop-about to process"); while ((length = bufferedReader.read(content)) > -1) { processEvents(decoderInstance.decodeEvents(String.valueOf(content, 0, length))); } if (tailing) { try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } while (tailing); getLogger().debug("processing complete: " + fileURL); shutdown(); } private void processEvents(Collection c) { if (c == null) { return; } for (Iterator iter = c.iterator(); iter.hasNext();) { LoggingEvent evt = (LoggingEvent) iter.next(); if (passesExpression(evt)) { if (evt.getProperty(Constants.HOSTNAME_KEY) != null) { evt.setProperty(Constants.HOSTNAME_KEY, host); } if (evt.getProperty(Constants.APPLICATION_KEY) != null) { evt.setProperty(Constants.APPLICATION_KEY, path); } doPost(evt); } } } /** * When true, this property uses the current Thread to perform the import, otherwise when false * (the default), a new Thread is created and started to manage the import. * * @return */ public final boolean isUseCurrentThread() { return useCurrentThread; } /** * Sets whether the current Thread or a new Thread is created to perform the import, the default * being false (new Thread created). * * @param useCurrentThread */ public final void setUseCurrentThread(boolean useCurrentThread) { this.useCurrentThread = useCurrentThread; } } hslogger-1.3.1.0/contrib/java/org/haskell/hslogger/XMLDecoder.java0000755000000000000000000003127107346545000023062 0ustar0000000000000000/* * Copyright 1999,2004 The Apache Software Foundation. * * 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. */ package org.haskell.hslogger4j; import java.awt.Component; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.StringReader; import java.net.URL; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import java.util.Vector; import javax.swing.ProgressMonitorInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.log4j.xml.*; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.spi.Decoder; import org.apache.log4j.spi.LoggingEvent; import org.apache.log4j.spi.ThrowableInformation; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * Decodes Logging Events in XML formated into elements that are used by * Chainsaw. * * This decoder can process a collection of log4j:event nodes ONLY * (no XML declaration nor eventSet node) * * NOTE: Only a single LoggingEvent is returned from the decode method * even though the DTD supports multiple events nested in an eventSet. * * @author Scott Deboy * @author Paul Smith * */ public class XMLDecoder implements Decoder { private static final String BEGINPART = ""; private static final String ENDPART = ""; private static final String RECORD_END = ""; private DocumentBuilderFactory dbf; private DocumentBuilder docBuilder; private Map additionalProperties = new HashMap(); private String partialEvent; private Component owner = null; public XMLDecoder(Component owner) { this(); this.owner = owner; } public XMLDecoder() { dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); try { docBuilder = dbf.newDocumentBuilder(); docBuilder.setErrorHandler(new SAXErrorHandler()); docBuilder.setEntityResolver(new Log4jEntityResolver()); } catch (ParserConfigurationException pce) { System.err.println("Unable to get document builder"); } } /** * Sets an additionalProperty map, where each Key/Value pair is * automatically added to each LoggingEvent as it is decoded. * * This is useful, say, to include the source file name of the Logging events * @param additionalProperties */ public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } /** * Converts the LoggingEvent data in XML string format into an actual * XML Document class instance. * @param data * @return dom document */ private Document parse(String data) { if (docBuilder == null || data == null) { return null; } Document document = null; try { // we change the system ID to a valid URI so that Crimson won't // complain. Indeed, "log4j.dtd" alone is not a valid URI which // causes Crimson to barf. The Log4jEntityResolver only cares // about the "log4j.dtd" ending. // buf.setLength(0); /** * resetting the length of the StringBuffer is dangerous, particularly * on some JDK 1.4 impls, there's a known Bug that causes a memory leak */ StringBuffer buf = new StringBuffer(1024); buf.append(BEGINPART); buf.append(data); buf.append(ENDPART); InputSource inputSource = new InputSource(new StringReader(buf.toString())); inputSource.setSystemId("dummy://log4j.dtd"); document = docBuilder.parse(inputSource); } catch (Exception e) { e.printStackTrace(); } return document; } /** * Decodes a File into a Vector of LoggingEvents * @param url the url of a file containing events to decode * @return Vector of LoggingEvents * @throws IOException */ public Vector decode(URL url) throws IOException { LineNumberReader reader = null; if (owner != null) { reader = new LineNumberReader(new InputStreamReader(new ProgressMonitorInputStream(owner, "Loading " + url , url.openStream()))); } else { reader = new LineNumberReader(new InputStreamReader(url.openStream())); } Vector v = new Vector(); String line = null; Vector events = null; try { while ((line = reader.readLine()) != null) { StringBuffer buffer = new StringBuffer(line); for (int i = 0;i<1000;i++) { buffer.append(reader.readLine()).append("\n"); } events = decodeEvents(buffer.toString()); if (events != null) v.addAll(events); } } finally { partialEvent = null; try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } return v; } public Vector decodeEvents(String document) { if (document != null) { if (document.trim().equals("")) { return null; } String newDoc=null; String newPartialEvent=null; //separate the string into the last portion ending with (which will //be processed) and the partial event which will be combined and processed in the next section //if the document does not contain a record end, append it to the partial event string if (document.lastIndexOf(RECORD_END) == -1) { partialEvent = partialEvent + document; return null; } if (document.lastIndexOf(RECORD_END) + RECORD_END.length() < document.length()) { newDoc = document.substring(0, document.lastIndexOf(RECORD_END) + RECORD_END.length()); newPartialEvent = document.substring(document.lastIndexOf(RECORD_END) + RECORD_END.length()); } else { newDoc = document; } if (partialEvent != null) { newDoc=partialEvent + newDoc; } partialEvent=newPartialEvent; Document doc = parse(newDoc); if (doc == null) { return null; } return decodeEvents(doc); } return null; } /** * Converts the string data into an XML Document, and then soaks out the * relevant bits to form a new LoggingEvent instance which can be used * by any Log4j element locally. * @param data * @return a single LoggingEvent */ public LoggingEvent decode(String data) { Document document = parse(data); if (document == null) { return null; } Vector events = decodeEvents(document); if (events.size() > 0) { return (LoggingEvent) events.firstElement(); } return null; } /** * Given a Document, converts the XML into a Vector of LoggingEvents * @param document * @return Vector of LoggingEvents */ private Vector decodeEvents(Document document) { Vector events = new Vector(); Logger logger = null; long timeStamp = 0L; String level = null; String threadName = null; Object message = null; String ndc = null; String[] exception = null; String className = null; String methodName = null; String fileName = null; String lineNumber = null; Hashtable properties = null; NodeList nl = document.getElementsByTagName("log4j:eventSet"); Node eventSet = nl.item(0); NodeList eventList = eventSet.getChildNodes(); for (int eventIndex = 0; eventIndex < eventList.getLength(); eventIndex++) { Node eventNode = eventList.item(eventIndex); //ignore carriage returns in xml if(eventNode.getNodeType() != Node.ELEMENT_NODE) { continue; } logger = Logger.getLogger( eventNode.getAttributes().getNamedItem("logger").getNodeValue()); timeStamp = Long.parseLong( eventNode.getAttributes().getNamedItem("timestamp").getNodeValue()); level =eventNode.getAttributes().getNamedItem("level").getNodeValue(); threadName = eventNode.getAttributes().getNamedItem("thread").getNodeValue(); NodeList list = eventNode.getChildNodes(); int listLength = list.getLength(); for (int y = 0; y < listLength; y++) { String tagName = list.item(y).getNodeName(); if (tagName.equalsIgnoreCase("log4j:message")) { message = getCData(list.item(y)); } if (tagName.equalsIgnoreCase("log4j:NDC")) { ndc = getCData(list.item(y)); } //still support receiving of MDC and convert to properties if (tagName.equalsIgnoreCase("log4j:MDC")) { properties = new Hashtable(); NodeList propertyList = list.item(y).getChildNodes(); int propertyLength = propertyList.getLength(); for (int i = 0; i < propertyLength; i++) { String propertyTag = propertyList.item(i).getNodeName(); if (propertyTag.equalsIgnoreCase("log4j:data")) { Node property = propertyList.item(i); String name = property.getAttributes().getNamedItem("name").getNodeValue(); String value = property.getAttributes().getNamedItem("value").getNodeValue(); properties.put(name, value); } } } if (tagName.equalsIgnoreCase("log4j:throwable")) { exception = new String[] { getCData(list.item(y)) }; } if (tagName.equalsIgnoreCase("log4j:properties")) { if (properties == null) { properties = new Hashtable(); } NodeList propertyList = list.item(y).getChildNodes(); int propertyLength = propertyList.getLength(); for (int i = 0; i < propertyLength; i++) { String propertyTag = propertyList.item(i).getNodeName(); if (propertyTag.equalsIgnoreCase("log4j:data")) { Node property = propertyList.item(i); String name = property.getAttributes().getNamedItem("name").getNodeValue(); String value = property.getAttributes().getNamedItem("value").getNodeValue(); properties.put(name, value); } } } /** * We add all the additional properties to the properties * hashtable. Don't override properties that already exist */ if (additionalProperties.size() > 0) { if (properties == null) { properties = new Hashtable(additionalProperties); } else { Iterator i = additionalProperties.entrySet().iterator(); while (i.hasNext()) { Map.Entry e = (Map.Entry) i.next(); if (!(properties.containsKey(e.getKey()))) { properties.put(e.getKey(), e.getValue()); } } } } } Level levelImpl = HsloggerLevel.toLevel(level); if (exception == null) { exception = new String[]{""}; } LoggingEvent loggingEvent = new LoggingEvent(); loggingEvent.setLogger(logger); loggingEvent.setTimeStamp(timeStamp); loggingEvent.setLevel(levelImpl); loggingEvent.setThreadName(threadName); loggingEvent.setMessage(message); loggingEvent.setNDC(ndc); loggingEvent.setThrowableInformation(new ThrowableInformation(exception)); loggingEvent.setProperties(properties); events.add(loggingEvent); logger = null; timeStamp = 0L; level = null; threadName = null; message = null; ndc = null; exception = null; className = null; methodName = null; fileName = null; lineNumber = null; properties = null; } return events; } private String getCData(Node n) { StringBuffer buf = new StringBuffer(); NodeList nl = n.getChildNodes(); for (int x = 0; x < nl.getLength(); x++) { Node innerNode = nl.item(x); if ( (innerNode.getNodeType() == Node.TEXT_NODE) || (innerNode.getNodeType() == Node.CDATA_SECTION_NODE)) { buf.append(innerNode.getNodeValue()); } } return buf.toString(); } } hslogger-1.3.1.0/hslogger.cabal0000644000000000000000000000534307346545000014460 0ustar0000000000000000cabal-version: 1.12 build-type: Simple name: hslogger version: 1.3.1.0 maintainer: hvr@gnu.org author: John Goerzen copyright: Copyright (c) 2004-2018 John Goerzen , (c) 2019 Herbert Valerio Riedel license: BSD3 license-file: LICENSE homepage: https://github.com/hvr/hslogger/wiki bug-reports: https://github.com/hvr/hslogger/issues category: Interfaces synopsis: Versatile logging framework description: @hslogger@ is a logging framework for Haskell, roughly similar to [Python's logging module](https://docs.python.org/2/library/logging.html). . @hslogger@ lets each log message have a priority and source be associated with it. The programmer can then define global handlers that route or filter messages based on the priority and source. @hslogger@ also has a [Syslog](https://tools.ietf.org/html/rfc5424) handler built in. extra-source-files: LICENSE CHANGELOG.md contrib/java/build.xml contrib/java/hslogger4j.jar contrib/java/hslogger4j-plugins.xml contrib/java/org/haskell/hslogger/HsloggerLevel.java contrib/java/org/haskell/hslogger/LogFileXMLReceiver.java contrib/java/org/haskell/hslogger/XMLDecoder.java testsrc/Tests.hs testsrc/runtests.hs tested-with: GHC ==7.0.4 || ==7.2.2 || ==7.4.2 || ==7.6.3 || ==7.8.4 || ==7.10.3 || ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.4 source-repository head type: git location: http://github.com/hvr/hslogger.git flag network--GT-3_0_0 description: [network](http://hackage.haskell.org/package/network) ≥ 3.0.0 default: True manual: False library hs-source-dirs: src exposed-modules: System.Log System.Log.Handler System.Log.Formatter System.Log.Handler.Simple System.Log.Handler.Syslog System.Log.Handler.Growl System.Log.Handler.Log4jXML System.Log.Logger other-modules: UTF8 default-language: Haskell2010 other-extensions: CPP ExistentialQuantification DeriveDataTypeable build-depends: base >= 4.3 && < 4.14 , bytestring >= 0.9 && < 0.11 , containers >= 0.4 && < 0.7 , deepseq >= 1.1 && < 1.5 , time >= 1.2 && < 1.10 , old-locale >= 1.0 && < 1.1 if flag(network--GT-3_0_0) build-depends: network-bsd >= 2.8.1 && <2.9, network >= 3.0 && <3.2 else build-depends: network >= 2.6 && <2.9 if !os(windows) Build-Depends: unix >= 2.4.2 && < 2.8 if !impl(ghc >= 7.6) build-depends: ghc-prim test-suite runtests type: exitcode-stdio-1.0 hs-source-dirs: testsrc main-is: runtests.hs other-modules: Tests default-language: Haskell2010 build-depends: base , HUnit == 1.3.* || == 1.6.* , hslogger hslogger-1.3.1.0/src/System/0000755000000000000000000000000007346545000013730 5ustar0000000000000000hslogger-1.3.1.0/src/System/Log.hs0000644000000000000000000000351607346545000015012 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} #if __GLASGOW_HASKELL__ >= 702 {-# LANGUAGE DeriveGeneric #-} #endif {- | Module : System.Log Copyright : Copyright (C) 2004-2011 John Goerzen License : BSD3 Portability: portable Haskell Logging Framework Written by John Goerzen, jgoerzen\@complete.org This module defines basic types used for logging. Extensive documentation is available in "System.Log.Logger". -} module System.Log(-- * Types Priority(..), LogRecord ) where import Control.DeepSeq (NFData(rnf)) import Data.Data (Data, Typeable) #if __GLASGOW_HASKELL__ >= 702 import GHC.Generics (Generic) #endif {- | Priorities are used to define how important a log message is. Users can filter log messages based on priorities. These have their roots on the traditional syslog system. The standard definitions are given below, but you are free to interpret them however you like. They are listed here in ascending importance order. -} data Priority = DEBUG -- ^ Debug messages | INFO -- ^ Information | NOTICE -- ^ Normal runtime conditions | WARNING -- ^ General Warnings | ERROR -- ^ General Errors | CRITICAL -- ^ Severe situations | ALERT -- ^ Take immediate action | EMERGENCY -- ^ System is unusable #if __GLASGOW_HASKELL__ >= 702 deriving (Eq, Ord, Enum, Bounded, Show, Read, Data, Typeable, Generic) #else deriving (Eq, Ord, Enum, Bounded, Show, Read, Data, Typeable) #endif -- | @since 1.3.1.0 instance NFData Priority where rnf = (`seq` ()) {- | Internal type of log records -} type LogRecord = (Priority, String) hslogger-1.3.1.0/src/System/Log/0000755000000000000000000000000007346545000014451 5ustar0000000000000000hslogger-1.3.1.0/src/System/Log/Formatter.hs0000644000000000000000000001066607346545000016761 0ustar0000000000000000{-# LANGUAGE CPP #-} {- Copyright (c) 2005-2011 John Goerzen License: BSD3 -} {- | Definition of log formatter support A few basic, and extendable formatters are defined. Please see "System.Log.Logger" for extensive documentation on the logging system. -} module System.Log.Formatter( LogFormatter , nullFormatter , simpleLogFormatter , tfLogFormatter , varFormatter ) where import Data.List import Control.Applicative ((<$>)) import Control.Concurrent (myThreadId) #ifndef mingw32_HOST_OS import System.Posix.Process (getProcessID) #endif #if MIN_VERSION_time(1,5,0) import Data.Time.Format (defaultTimeLocale) #else import System.Locale (defaultTimeLocale) #endif import Data.Time (getZonedTime,getCurrentTime,formatTime) import System.Log -- | A LogFormatter is used to format log messages. Note that it is paramterized on the -- 'Handler' to allow the formatter to use information specific to the handler -- (an example of can be seen in the formatter used in 'System.Log.Handler.Syslog') type LogFormatter a = a -- ^ The LogHandler that the passed message came from -> LogRecord -- ^ The log message and priority -> String -- ^ The logger name -> IO String -- ^ The formatted log message -- | Returns the passed message as is, ie. no formatting is done. nullFormatter :: LogFormatter a nullFormatter _ (_,msg) _ = return msg -- | Takes a format string, and returns a formatter that may be used to -- format log messages. The format string may contain variables prefixed with -- a $-sign which will be replaced at runtime with corresponding values. The -- currently supported variables are: -- -- * @$msg@ - The actual log message -- -- * @$loggername@ - The name of the logger -- -- * @$prio@ - The priority level of the message -- -- * @$tid@ - The thread ID -- -- * @$pid@ - Process ID (Not available on windows) -- -- * @$time@ - The current time -- -- * @$utcTime@ - The current time in UTC Time simpleLogFormatter :: String -> LogFormatter a simpleLogFormatter format h (prio, msg) loggername = tfLogFormatter "%F %X %Z" format h (prio,msg) loggername -- | Like 'simpleLogFormatter' but allow the time format to be specified in the first -- parameter (this is passed to 'Date.Time.Format.formatTime') tfLogFormatter :: String -> String -> LogFormatter a tfLogFormatter timeFormat format = do varFormatter [("time", formatTime defaultTimeLocale timeFormat <$> getZonedTime) ,("utcTime", formatTime defaultTimeLocale timeFormat <$> getCurrentTime) ] format -- | An extensible formatter that allows new substition /variables/ to be defined. -- Each variable has an associated IO action that is used to produce the -- string to substitute for the variable name. The predefined variables are the same -- as for 'simpleLogFormatter' /excluding/ @$time@ and @$utcTime@. varFormatter :: [(String, IO String)] -> String -> LogFormatter a varFormatter vars format _h (prio,msg) loggername = do outmsg <- replaceVarM (vars++[("msg", return msg) ,("prio", return $ show prio) ,("loggername", return loggername) ,("tid", show <$> myThreadId) #ifndef mingw32_HOST_OS ,("pid", show <$> getProcessID) #endif ] ) format return outmsg -- | Replace some '$' variables in a string with supplied values replaceVarM :: [(String, IO String)] -- ^ A list of (variableName, action to get the replacement string) pairs -> String -- ^ String to perform substitution on -> IO String -- ^ Resulting string replaceVarM _ [] = return [] replaceVarM keyVals (s:ss) | s=='$' = do (f,rest) <- replaceStart keyVals ss repRest <- replaceVarM keyVals rest return $ f ++ repRest | otherwise = replaceVarM keyVals ss >>= return . (s:) where replaceStart [] str = return ("$",str) replaceStart ((k,v):kvs) str | k `isPrefixOf` str = do vs <- v return (vs, drop (length k) str) | otherwise = replaceStart kvs str hslogger-1.3.1.0/src/System/Log/Handler.hs0000644000000000000000000000403407346545000016363 0ustar0000000000000000{- | Module : System.Log.Handler Copyright : Copyright (C) 2004-2011 John Goerzen License : BSD3 Portability: portable Definition of log handler support For some handlers, check out "System.Log.Handler.Simple" and "System.Log.Handler.Syslog". Please see "System.Log.Logger" for extensive documentation on the logging system. Written by John Goerzen, jgoerzen\@complete.org -} module System.Log.Handler(-- * Basic Types LogHandler(..) ) where import System.Log import System.Log.Formatter {- | All log handlers should adhere to this. -} {- | This is the base class for the various log handlers. They should all adhere to this class. -} class LogHandler a where -- | Sets the log level. 'handle' will drop -- items beneath this level. setLevel :: a -> Priority -> a -- | Gets the current level. getLevel :: a -> Priority -- | Set a log formatter to customize the log format for this Handler setFormatter :: a -> LogFormatter a -> a getFormatter :: a -> LogFormatter a getFormatter _ = nullFormatter -- | Logs an event if it meets the requirements -- given by the most recent call to 'setLevel'. handle :: a -> LogRecord -> String-> IO () handle h (pri, msg) logname = if pri >= (getLevel h) then do formattedMsg <- (getFormatter h) h (pri,msg) logname emit h (pri, formattedMsg) logname else return () -- | Forces an event to be logged regardless of -- the configured level. emit :: a -> LogRecord -> String -> IO () -- | Closes the logging system, causing it to close -- any open files, etc. close :: a -> IO () hslogger-1.3.1.0/src/System/Log/Handler/0000755000000000000000000000000007346545000016026 5ustar0000000000000000hslogger-1.3.1.0/src/System/Log/Handler/Growl.hs0000644000000000000000000001151107346545000017453 0ustar0000000000000000{- | Module : System.Log.Handler.Growl Copyright : Copyright (C) 2007-2011 John Goerzen License : BSD3 Portability: portable Simple log handlers Written by Richard M. Neswold, Jr. rich.neswold\@gmail.com -} module System.Log.Handler.Growl(addTarget, growlHandler) where import Data.Char import Data.Word import qualified Network.Socket as S import qualified Network.Socket.ByteString as SBS import qualified Network.BSD as S import System.Log import System.Log.Handler import System.Log.Formatter import UTF8 sendTo :: S.Socket -> String -> S.SockAddr -> IO Int sendTo s str = SBS.sendTo s (toUTF8BS str) data GrowlHandler = GrowlHandler { priority :: Priority, formatter :: LogFormatter GrowlHandler, appName :: String, skt :: S.Socket, targets :: [S.HostAddress] } instance LogHandler GrowlHandler where setLevel gh p = gh { priority = p } getLevel = priority setFormatter gh f = gh { formatter = f } getFormatter = formatter emit gh lr _ = let pkt = buildNotification gh nmGeneralMsg lr in mapM_ (sendNote (skt gh) pkt) (targets gh) close gh = let pkt = buildNotification gh nmClosingMsg (WARNING, "Connection closing.") s = skt gh in mapM_ (sendNote s pkt) (targets gh) >> S.close s sendNote :: S.Socket -> String -> S.HostAddress -> IO Int sendNote s pkt ha = sendTo s pkt (S.SockAddrInet 9887 ha) -- Right now there are two "notification names": "message" and -- "disconnecting". All log messages are sent using the "message" -- name. When the handler gets closed properly, the "disconnecting" -- notification gets sent. nmGeneralMsg :: String nmGeneralMsg = "message" nmClosingMsg :: String nmClosingMsg = "disconnecting" {- | Creates a Growl handler. Once a Growl handler has been created, machines that are to receive the message have to be specified. -} growlHandler :: String -- ^ The name of the service -> Priority -- ^ Priority of handler -> IO GrowlHandler growlHandler nm pri = do { s <- S.socket S.AF_INET S.Datagram 0 ; return GrowlHandler { priority = pri, appName = nm, formatter=nullFormatter, skt = s, targets = [] } } -- Converts a Word16 into a string of two characters. The value is -- emitted in network byte order. emit16 :: Word16 -> String emit16 v = let (h, l) = (fromEnum v) `divMod` 256 in [chr h, chr l] emitLen16 :: [a] -> String emitLen16 = emit16 . fromIntegral . length -- Takes a Service record and generates a network packet -- representing the service. buildRegistration :: GrowlHandler -> String buildRegistration s = concat fields where fields = [ ['\x1', '\x4'], emitLen16 (appName s), emitLen8 appNotes, emitLen8 appNotes, appName s, foldl packIt [] appNotes, ['\x0' .. (chr (length appNotes - 1))] ] packIt a b = a ++ (emitLen16 b) ++ b appNotes = [ nmGeneralMsg, nmClosingMsg ] emitLen8 v = [chr $ length v] {- | Adds a remote machine's address to the list of targets that will receive log messages. Calling this function sends a registration packet to the machine. This function will throw an exception if the host name cannot be found. -} addTarget :: S.HostName -> GrowlHandler -> IO GrowlHandler addTarget hn gh = do { he <- S.getHostByName hn ; let ha = S.hostAddress he sa = S.SockAddrInet 9887 ha in do { _ <- sendTo (skt gh) (buildRegistration gh) sa ; return gh { targets = ha:(targets gh) } } } -- Converts a Priority type into the subset of integers needed in the -- network packet's flag field. toFlags :: Priority -> Word16 toFlags DEBUG = 12 toFlags INFO = 10 toFlags NOTICE = 0 toFlags WARNING = 2 toFlags ERROR = 3 -- Same as WARNING, but "sticky" bit set toFlags CRITICAL = 3 -- Same as WARNING, but "sticky" bit set toFlags ALERT = 4 toFlags EMERGENCY = 5 -- Same as ALERT, but "sticky" bit set -- Creates a network packet containing a notification record. buildNotification :: GrowlHandler -> String -> LogRecord -> String buildNotification gh nm (p, msg) = concat fields where fields = [ ['\x1', '\x5'], emit16 (toFlags p), emitLen16 nm, emit16 0, emitLen16 msg, emitLen16 (appName gh), nm, [], msg, appName gh ] hslogger-1.3.1.0/src/System/Log/Handler/Log4jXML.hs0000644000000000000000000002021207346545000017717 0ustar0000000000000000{-# LANGUAGE CPP #-} {- | Module : System.Log.Handler.Log4jXML Copyright : Copyright (C) 2007-2011 John Goerzen License : BSD3 Portability: GHC only? log4j[1] XMLLayout log handlers. Written by Bjorn Buckwalter, bjorn.buckwalter\@gmail.com -} module System.Log.Handler.Log4jXML ( -- * Introduction {- | This module provides handlers for hslogger that are compatible with log4j's XMLLayout. In particular log messages created by the handlers can be published directly to the GUI-based log viewer Chainsaw v2[2]. The set of log levels in hslogger is richer than the basic set of log4j levels. Two sets of handlers are provided with hslogger4j, one which produces logs with hslogger's levels and one which \"demotes\" them to the basic log4j levels. If full hslogger levels are used some Java installation (see below) is necessary to make Chainsaw aware of them. Usage of the handlers in hslogger4j is analoguous to usage of the 'System.Log.Handler.Simple.StreamHandler' and 'System.Log.Handler.Simple.FileHandler' in "System.Log.Handler.Simple". The following handlers are provided: -} -- ** Handlers with hslogger levels log4jStreamHandler, log4jFileHandler, -- ** Handlers with log4j levels log4jStreamHandler', log4jFileHandler' -- * Java install process {- | This is only necessary if you want to use the hslogger levels. Add @hslogger4j.jar@ from @contrib\/java@ to your classpath. To use you will also need to have the jars @log4j-1.3alpha-7.jar@ and @log4j-xml-1.3alpha-7.jar@ that are distributed with Chainsaw on your classpath. (On Mac OS X I added all three jars to @~\/Library\/Java\/Extensions@. It seems that it is not sufficient that Chainsaw already includes its jars in the classpath when launching - perhaps the plugin classloader does not inherit Chainsaw's classpath. Adding the jars to @~\/.chainsaw\/plugins@ wouldn't work either.) If for whatever reason you have to rebuild the hslogger4j jar just run @ant@[3] in the @contrib\/java@ directory. The new jar will be created in the @contrib\/java\/dist@ directory. The Java source code is copyright The Apache Software Foundation and licensed under the Apache Licence version 2.0. -} -- * Chainsaw setup {- | If you are only using the basic log4j levels just use Chainsaw's regular facilities to browse logs or listen for log messages (e.g. @XMLSocketReceiver@). If you want to use the hslogger levels the easiest way to set up Chainsaw is to load the plugins in @hslogger4j-plugins.xml@ in @contrib\/java@ when launching Chainsaw. Two receivers will be defined, one that listens for logmessages and one for reading log files. Edit the properties of those receivers as needed (e.g. @port@, @fileURL@) and restart them. You will also want to modify Chainsaw's formatting preferences to display levels as text instead of icons. -} -- * Example usage {- | In the IO monad: > lh2 <- log4jFileHandler "log.xml" DEBUG > updateGlobalLogger rootLoggerName (addHandler lh2) > h <- connectTo "localhost" (PortNumber 4448) > lh <- log4jStreamHandler h NOTICE > updateGlobalLogger rootLoggerName (addHandler lh) -} -- * References {- | (1) (2) (3) -} ) where import Control.Concurrent (myThreadId) -- myThreadId is GHC only! import Data.List (isPrefixOf) import System.IO #if MIN_VERSION_time(1,5,0) import Data.Time.Format (defaultTimeLocale) #else import System.Locale (defaultTimeLocale) #endif import Data.Time import System.Log import System.Log.Handler import System.Log.Handler.Simple (streamHandler, GenericHandler(..)) -- Handler that logs to a handle rendering message priorities according -- to the supplied function. log4jHandler :: (Priority -> String) -> Handle -> Priority -> IO (GenericHandler Handle) log4jHandler showPrio h pri = do hndlr <- streamHandler h pri return $ setFormatter hndlr xmlFormatter where -- A Log Formatter that creates an XML element representing a log4j event/message. xmlFormatter :: a -> (Priority,String) -> String -> IO String xmlFormatter _ (prio,msg) logger = do time <- getCurrentTime thread <- myThreadId return . show $ Elem "log4j:event" [ ("logger" , logger ) , ("timestamp", millis time ) , ("level" , showPrio prio) , ("thread" , show thread ) ] (Just $ Elem "log4j:message" [] (Just $ CDATA msg)) where -- This is an ugly hack to get a unix epoch with milliseconds. -- The use of "take 3" causes the milliseconds to always be -- rounded downwards, which I suppose may be the expected -- behaviour for time. millis t = formatTime defaultTimeLocale "%s" t ++ (take 3 $ formatTime defaultTimeLocale "%q" t) -- | Create a stream log handler that uses hslogger priorities. log4jStreamHandler :: Handle -> Priority -> IO (GenericHandler Handle) log4jStreamHandler = log4jHandler show {- | Create a stream log handler that uses log4j levels (priorities). The priorities of messages are shoehorned into log4j levels as follows: @ DEBUG -> DEBUG INFO, NOTICE -> INFO WARNING -> WARN ERROR, CRITICAL, ALERT -> ERROR EMERGENCY -> FATAL @ This is useful when the log will only be consumed by log4j tools and you don't want to go out of your way transforming the log or configuring the tools. -} log4jStreamHandler' :: Handle -> Priority -> IO (GenericHandler Handle) log4jStreamHandler' = log4jHandler show' where show' :: Priority -> String show' NOTICE = "INFO" show' WARNING = "WARN" show' CRITICAL = "ERROR" show' ALERT = "ERROR" show' EMERGENCY = "FATAL" show' p = show p -- Identical for DEBUG, INFO, ERROR. -- | Create a file log handler that uses hslogger priorities. log4jFileHandler :: FilePath -> Priority -> IO (GenericHandler Handle) log4jFileHandler fp pri = do h <- openFile fp AppendMode sh <- log4jStreamHandler h pri return (sh{closeFunc = hClose}) {- | Create a file log handler that uses log4j levels (see 'log4jStreamHandler'' for mappings). -} log4jFileHandler' :: FilePath -> Priority -> IO (GenericHandler Handle) log4jFileHandler' fp pri = do h <- openFile fp AppendMode sh <- log4jStreamHandler' h pri return (sh{closeFunc = hClose}) -- A type for building and showing XML elements. Could use a fancy XML -- library but am reluctant to introduce dependencies. data XML = Elem String [(String, String)] (Maybe XML) | CDATA String instance Show XML where show (CDATA s) = "" where escapeCDATA = replace "]]>" "]]<" -- The best we can do, I guess. show (Elem name attrs child) = "<" ++ name ++ showAttrs attrs ++ showChild child where showAttrs [] = "" showAttrs ((k,v):as) = " " ++ k ++ "=\"" ++ escapeAttr v ++ "\"" ++ showAttrs as where escapeAttr = replace "\"" """ . replace "<" "<" . replace "&" "&" showChild Nothing = "/>" showChild (Just c) = ">" ++ show c ++ "" -- Replaces instances of first list by second list in third list. -- Definition blatantly stoled from jethr0's comment at -- http://bluebones.net/2007/01/replace-in-haskell/. Can be swapped -- with definition (or import) from MissingH. replace :: Eq a => [a] -> [a] -> [a] -> [a] replace _ _ [ ] = [] replace from to xs@(a:as) = if isPrefixOf from xs then to ++ drop (length from) xs else a : replace from to as hslogger-1.3.1.0/src/System/Log/Handler/Simple.hs0000644000000000000000000000701707346545000017620 0ustar0000000000000000{- | Module : System.Log.Handler.Simple Copyright : Copyright (C) 2004-2011 John Goerzen License : BSD3 Portability: portable Simple log handlers Written by John Goerzen, jgoerzen\@complete.org -} module System.Log.Handler.Simple(streamHandler, fileHandler, GenericHandler (..), verboseStreamHandler) where import Control.Exception (tryJust) import Control.DeepSeq import Data.Char (ord) import System.Log import System.Log.Handler import System.Log.Formatter import System.IO import System.IO.Error import Control.Concurrent.MVar {- | A helper data type. -} data GenericHandler a = GenericHandler {priority :: Priority, formatter :: LogFormatter (GenericHandler a), privData :: a, writeFunc :: a -> String -> IO (), closeFunc :: a -> IO () } instance LogHandler (GenericHandler a) where setLevel sh p = sh{priority = p} getLevel sh = priority sh setFormatter sh f = sh{formatter = f} getFormatter sh = formatter sh emit sh (_,msg) _ = (writeFunc sh) (privData sh) msg close sh = (closeFunc sh) (privData sh) {- | Create a stream log handler. Log messages sent to this handler will be sent to the stream used initially. Note that the 'close' method will have no effect on stream handlers; it does not actually close the underlying stream. -} streamHandler :: Handle -> Priority -> IO (GenericHandler Handle) streamHandler h pri = do lock <- newMVar () let mywritefunc hdl msg = msg `deepseq` withMVar lock (\_ -> do writeToHandle hdl msg hFlush hdl ) return (GenericHandler {priority = pri, formatter = nullFormatter, privData = h, writeFunc = mywritefunc, closeFunc = \_ -> return ()}) where writeToHandle hdl msg = do rv <- tryJust myException (hPutStrLn hdl msg) either (handleWriteException hdl msg) return rv myException e | isDoesNotExistError e = Just e | otherwise = Nothing handleWriteException hdl msg e = let msg' = "Error writing log message: " ++ show e ++ " (original message: " ++ msg ++ ")" in hPutStrLn hdl (encodingSave msg') encodingSave = concatMap (\c -> if ord c > 127 then "\\" ++ show (ord c) else [c]) {- | Create a file log handler. Log messages sent to this handler will be sent to the filename specified, which will be opened in Append mode. Calling 'close' on the handler will close the file. -} fileHandler :: FilePath -> Priority -> IO (GenericHandler Handle) fileHandler fp pri = do h <- openFile fp AppendMode sh <- streamHandler h pri return (sh{closeFunc = hClose}) {- | Like 'streamHandler', but note the priority and logger name along with each message. -} verboseStreamHandler :: Handle -> Priority -> IO (GenericHandler Handle) verboseStreamHandler h pri = let fmt = simpleLogFormatter "[$loggername/$prio] $msg" in do hndlr <- streamHandler h pri return $ setFormatter hndlr fmt hslogger-1.3.1.0/src/System/Log/Handler/Syslog.hs0000644000000000000000000002627007346545000017651 0ustar0000000000000000{-# LANGUAGE CPP #-} {- | Module : System.Log.Handler.Syslog Copyright : Copyright (C) 2004-2011 John Goerzen License : BSD3 Portability: portable Syslog handler for the Haskell Logging Framework Written by John Goerzen, jgoerzen\@complete.org This module implements an interface to the Syslog service commonly found in Unix\/Linux systems. This interface is primarily of interest to developers of servers, as Syslog does not typically display messages in an interactive fashion. This module is written in pure Haskell and is capable of logging to a local or remote machine using the Syslog protocol. You can create a new Syslog 'LogHandler' by calling 'openlog'. More information on the Haskell Logging Framework can be found at "System.Log.Logger". This module can also be used outside of the rest of that framework for those interested in that. -} module System.Log.Handler.Syslog( SyslogHandler, -- No constructors. -- * Handler Initialization openlog, -- * Advanced handler initialization #ifndef mingw32_HOST_OS openlog_local, #endif openlog_remote, openlog_generic, -- * Data Types Facility(..), Option(..) ) where import qualified Control.Exception as E import System.Log import System.Log.Formatter import System.Log.Handler import Data.Bits import qualified Network.Socket as S import qualified Network.Socket.ByteString as SBS import qualified Network.BSD as S import Data.List (genericDrop) #ifndef mingw32_HOST_OS import System.Posix.Process(getProcessID) #endif import System.IO import Control.Monad (void, when) import UTF8 send :: S.Socket -> String -> IO Int send s = SBS.send s . toUTF8BS sendTo :: S.Socket -> String -> S.SockAddr -> IO Int sendTo s str = SBS.sendTo s (toUTF8BS str) code_of_pri :: Priority -> Int code_of_pri p = case p of EMERGENCY -> 0 ALERT -> 1 CRITICAL -> 2 ERROR -> 3 WARNING -> 4 NOTICE -> 5 INFO -> 6 DEBUG -> 7 {- | Facilities are used by the system to determine where messages are sent. -} data Facility = KERN -- ^ Kernel messages; you should likely never use this in your programs | USER -- ^ General userland messages. Use this if nothing else is appropriate | MAIL -- ^ E-Mail system | DAEMON -- ^ Daemon (server process) messages | AUTH -- ^ Authentication or security messages | SYSLOG -- ^ Internal syslog messages; you should likely never use this in your programs | LPR -- ^ Printer messages | NEWS -- ^ Usenet news | UUCP -- ^ UUCP messages | CRON -- ^ Cron messages | AUTHPRIV -- ^ Private authentication messages | FTP -- ^ FTP messages | LOCAL0 -- ^ LOCAL0 through LOCAL7 are reserved for you to customize as you wish | LOCAL1 | LOCAL2 | LOCAL3 | LOCAL4 | LOCAL5 | LOCAL6 | LOCAL7 deriving (Eq, Show, Read) code_of_fac :: Facility -> Int code_of_fac f = case f of KERN -> 0 USER -> 1 MAIL -> 2 DAEMON -> 3 AUTH -> 4 SYSLOG -> 5 LPR -> 6 NEWS -> 7 UUCP -> 8 CRON -> 9 AUTHPRIV -> 10 FTP -> 11 LOCAL0 -> 16 LOCAL1 -> 17 LOCAL2 -> 18 LOCAL3 -> 19 LOCAL4 -> 20 LOCAL5 -> 21 LOCAL6 -> 22 LOCAL7 -> 23 makeCode :: Facility -> Priority -> Int makeCode fac pri = let faccode = code_of_fac fac pricode = code_of_pri pri in (faccode `shiftL` 3) .|. pricode {- | Options for 'openlog'. -} data Option = PID -- ^ Automatically log process ID (PID) with each message | PERROR -- ^ Send a copy of each message to stderr deriving (Eq,Show,Read) data SyslogHandler = SyslogHandler {options :: [Option], facility :: Facility, identity :: String, logsocket :: S.Socket, address :: S.SockAddr, sock_type :: S.SocketType, priority :: Priority, formatter :: LogFormatter SyslogHandler } {- | Initialize the Syslog system using the local system's default interface, \/dev\/log. Will return a new 'System.Log.Handler.LogHandler'. On Windows, instead of using \/dev\/log, this will attempt to send UDP messages to something listening on the syslog port (514) on localhost. Use 'openlog_remote' if you need more control. -} openlog :: String -- ^ The name of this program -- will be prepended to every log message -> [Option] -- ^ A list of 'Option's. The list [] is perfectly valid. ['PID'] is probably most common here. -> Facility -- ^ The 'Facility' value to pass to the syslog system for every message logged -> Priority -- ^ Messages logged below this priority will be ignored. To include every message, set this to 'DEBUG'. -> IO SyslogHandler -- ^ Returns the new handler #ifdef mingw32_HOST_OS openlog = openlog_remote S.AF_INET "localhost" 514 #elif darwin_HOST_OS openlog = openlog_local "/var/run/syslog" #else openlog = openlog_local "/dev/log" #endif {- | Initialize the Syslog system using an arbitrary Unix socket (FIFO). Not supported under Windows. -} #ifndef mingw32_HOST_OS openlog_local :: String -- ^ Path to FIFO -> String -- ^ Program name -> [Option] -- ^ 'Option's -> Facility -- ^ Facility value -> Priority -- ^ Priority limit -> IO SyslogHandler openlog_local fifopath ident options' fac pri = do (s, t) <- do -- "/dev/log" is usually Datagram, -- but most of syslog loggers allow it to be -- of Stream type. glibc's" openlog()" -- does roughly the similar thing: -- http://www.gnu.org/software/libc/manual/html_node/openlog.html s <- S.socket S.AF_UNIX S.Stream 0 tryStream s `E.catch` (onIOException (fallbackToDgram s)) openlog_generic s (S.SockAddrUnix fifopath) t ident options' fac pri where onIOException :: IO a -> E.IOException -> IO a onIOException a _ = a tryStream :: S.Socket -> IO (S.Socket, S.SocketType) tryStream s = do S.connect s (S.SockAddrUnix fifopath) return (s, S.Stream) fallbackToDgram :: S.Socket -> IO (S.Socket, S.SocketType) fallbackToDgram s = do S.close s -- close Stream variant d <- S.socket S.AF_UNIX S.Datagram 0 return (d, S.Datagram) #endif {- | Log to a remote server via UDP. -} openlog_remote :: S.Family -- ^ Usually AF_INET or AF_INET6; see Network.Socket -> S.HostName -- ^ Remote hostname. Some use @localhost@ -> S.PortNumber -- ^ 514 is the default for syslog -> String -- ^ Program name -> [Option] -- ^ 'Option's -> Facility -- ^ Facility value -> Priority -- ^ Priority limit -> IO SyslogHandler openlog_remote fam hostname port ident options' fac pri = do he <- S.getHostByName hostname s <- S.socket fam S.Datagram 0 let addr = S.SockAddrInet port (head (S.hostAddresses he)) openlog_generic s addr S.Datagram ident options' fac pri {- | The most powerful initialization mechanism. Takes an open datagram socket. -} openlog_generic :: S.Socket -- ^ A datagram socket -> S.SockAddr -- ^ Address for transmissions -> S.SocketType -- ^ socket connection mode (stream / datagram) -> String -- ^ Program name -> [Option] -- ^ 'Option's -> Facility -- ^ Facility value -> Priority -- ^ Priority limit -> IO SyslogHandler openlog_generic sock addr sock_t ident opt fac pri = return (SyslogHandler {options = opt, facility = fac, identity = ident, logsocket = sock, address = addr, sock_type = sock_t, priority = pri, formatter = syslogFormatter }) syslogFormatter :: LogFormatter SyslogHandler syslogFormatter sh (p,msg) logname = let format = "[$loggername/$prio] $msg" in varFormatter [] format sh (p,msg) logname instance LogHandler SyslogHandler where setLevel sh p = sh{priority = p} getLevel sh = priority sh setFormatter sh f = sh{formatter = f} getFormatter sh = formatter sh emit sh (prio, msg) _ = do when (elem PERROR (options sh)) (hPutStrLn stderr msg) pidPart <- getPidPart void $ sendstr (toSyslogFormat msg pidPart) where sendstr :: String -> IO String sendstr [] = return [] sendstr omsg = do sent <- case sock_type sh of S.Datagram -> sendTo (logsocket sh) omsg (address sh) S.Stream -> send (logsocket sh) omsg sendstr (genericDrop sent omsg) toSyslogFormat msg' pidPart = "<" ++ code ++ ">" ++ identity' ++ pidPart ++ ": " ++ msg' ++ "\0" code = show $ makeCode (facility sh) prio identity' = identity sh getPidPart = if elem PID (options sh) then getPid >>= \pid -> return ("[" ++ pid ++ "]") else return "" getPid :: IO String getPid = #ifndef mingw32_HOST_OS getProcessID >>= return . show #else return "windows" #endif close sh = S.close (logsocket sh) hslogger-1.3.1.0/src/System/Log/Logger.hs0000644000000000000000000005232307346545000016231 0ustar0000000000000000{-# LANGUAGE CPP, ExistentialQuantification #-} {- | Module : System.Log.Logger Copyright : Copyright (C) 2004-2011 John Goerzen License : BSD3 Portability: portable Haskell Logging Framework, Primary Interface Written by John Goerzen, jgoerzen\@complete.org Welcome to the error and information logging system for Haskell. This system is patterned after Python\'s @logging@ module, and some of the documentation here was based on documentation there. To log a message, you perform operations on 'Logger's. Each 'Logger' has a name, and they are arranged hierarchically. Periods serve as separators. Therefore, a 'Logger' named \"foo\" is the parent of loggers \"foo.printing\", \"foo.html\", and \"foo.io\". These names can be anything you want. They're used to indicate the area of an application or library in which a logged message originates. Later you will see how you can use this concept to fine-tune logging behaviors based on specific application areas. You can also tune logging behaviors based upon how important a message is. Each message you log will have an importance associated with it. The different importance levels are given by the 'Priority' type. I've also provided some convenient functions that correspond to these importance levels: 'debugM' through 'emergencyM' log messages with the specified importance. Now, an importance level (or 'Priority') is associated not just with a particular message but also with a 'Logger'. If the 'Priority' of a given log message is lower than the 'Priority' configured in the 'Logger', that message is ignored. This way, you can globally control how verbose your logging output is. Now, let's follow what happens under the hood when you log a message. We'll assume for the moment that you are logging something with a high enough 'Priority' that it passes the test in your 'Logger'. In your code, you'll call 'logM' or something like 'debugM' to log the message. Your 'Logger' decides to accept the message. What next? Well, we also have a notion of /handlers/ ('LogHandler's, to be precise). A 'LogHandler' is a thing that takes a message and sends it somewhere. That \"somewhere\" may be your screen (via standard error), your system's logging infrastructure (via syslog), a file, or other things. Each 'Logger' can have zero or more 'LogHandler's associated with it. When your 'Logger' has a message to log, it passes it to every 'LogHandler' it knows of to process. What's more, it is also passed to /all handlers of all ancestors of the Logger/, regardless of whether those 'Logger's would normally have passed on the message. Each 'Logger' can /optionally/ store a 'Priority'. If a given Logger does not have a Priority, and you log a message to that logger, the system will use the priority of the parent of the destination logger to find out whether to log the message. If the parent has no priority associated with it, the system continues walking up the tree to figure out a priority until it hits the root logger. In this way, you can easily adjust the priority of an entire subtree of loggers. When a new logger is created, it has no priority by default. The exception is the root logger, which has a WARNING priority by default. To give you one extra little knob to turn, 'LogHandler's can also have importance levels ('Priority') associated with them in the same way that 'Logger's do. They act just like the 'Priority' value in the 'Logger's -- as a filter. It's useful, for instance, to make sure that under no circumstances will a mere 'DEBUG' message show up in your syslog. There are three built-in handlers given in two built-in modules: "System.Log.Handler.Simple" and "System.Log.Handler.Syslog". There is a special logger known as the /root logger/ that sits at the top of the logger hierarchy. It is always present, and handlers attached there will be called for every message. You can use 'getRootLogger' to get it or 'rootLoggerName' to work with it by name. The formatting of log messages may be customized by setting a 'LogFormatter' on the desired 'LogHandler'. There are a number of simple formatters defined in "System.Log.Formatter", which may be used directly, or extend to create your own formatter. Here's an example to illustrate some of these concepts: > import System.Log.Logger > import System.Log.Handler.Syslog > import System.Log.Handler.Simple > import System.Log.Handler (setFormatter) > import System.Log.Formatter > > -- By default, all messages of level WARNING and above are sent to stderr. > -- Everything else is ignored. > > -- "MyApp.Component" is an arbitrary string; you can tune > -- logging behavior based on it later. > main = do > debugM "MyApp.Component" "This is a debug message -- never to be seen" > warningM "MyApp.Component2" "Something Bad is about to happen." > > -- Copy everything to syslog from here on out. > s <- openlog "SyslogStuff" [PID] USER DEBUG > updateGlobalLogger rootLoggerName (addHandler s) > > errorM "MyApp.Component" "This is going to stderr and syslog." > > -- Now we'd like to see everything from BuggyComponent > -- at DEBUG or higher go to syslog and stderr. > -- Also, we'd like to still ignore things less than > -- WARNING in other areas. > -- > -- So, we adjust the Logger for MyApp.BuggyComponent. > > updateGlobalLogger "MyApp.BuggyComponent" > (setLevel DEBUG) > > -- This message will go to syslog and stderr > debugM "MyApp.BuggyComponent" "This buggy component is buggy" > > -- This message will go to syslog and stderr too. > warningM "MyApp.BuggyComponent" "Still Buggy" > > -- This message goes nowhere. > debugM "MyApp.WorkingComponent" "Hello" > > -- Now we decide we'd also like to log everything from BuggyComponent at DEBUG > -- or higher to a file for later diagnostics. We'd also like to customize the > -- format of the log message, so we use a 'simpleLogFormatter' > > h <- fileHandler "debug.log" DEBUG >>= \lh -> return $ > setFormatter lh (simpleLogFormatter "[$time : $loggername : $prio] $msg") > updateGlobalLogger "MyApp.BuggyComponent" (addHandler h) > > -- This message will go to syslog and stderr, > -- and to the file "debug.log" with a format like : > -- [2010-05-23 16:47:28 : MyApp.BuggyComponent : DEBUG] Some useful diagnostics... > debugM "MyApp.BuggyComponent" "Some useful diagnostics..." > > -} module System.Log.Logger( -- * Basic Types Logger, -- ** Re-Exported from System.Log Priority(..), -- * Logging Messages -- ** Basic logM, -- ** Utility Functions -- These functions are wrappers for 'logM' to -- make your job easier. debugM, infoM, noticeM, warningM, errorM, criticalM, alertM, emergencyM, removeAllHandlers, traplogging, -- ** Logging to a particular Logger by object logL, -- * Logger Manipulation {- | These functions help you work with loggers. There are some special things to be aware of. First of all, whenever you first access a given logger by name, it magically springs to life. It has a default 'Priority' of Nothing and an empty handler list -- which means that it will inherit whatever its parents do. -} -- ** Finding \/ Creating Loggers getLogger, getRootLogger, rootLoggerName, -- ** Modifying Loggers {- | Keep in mind that \"modification\" here is modification in the Haskell sense. We do not actually cause mutation in a specific 'Logger'. Rather, we return you a new 'Logger' object with the change applied. Also, please note that these functions will not have an effect on the global 'Logger' hierarchy. You may use your new 'Logger's locally, but other functions won't see the changes. To make a change global, you'll need to use 'updateGlobalLogger' or 'saveGlobalLogger'. -} addHandler, removeHandler, setHandlers, getLevel, setLevel, clearLevel, -- ** Saving Your Changes {- | These functions commit changes you've made to loggers to the global logger hierarchy. -} saveGlobalLogger, updateGlobalLogger ) where import System.Log import System.Log.Handler(LogHandler, close) import System.Log.Formatter(LogFormatter) -- for Haddock import qualified System.Log.Handler(handle) import System.Log.Handler.Simple import System.IO import System.IO.Unsafe import Control.Concurrent.MVar import Data.List(map, isPrefixOf) import Data.Maybe import qualified Data.Map as Map import qualified Control.Exception --------------------------------------------------------------------------- -- Basic logger types --------------------------------------------------------------------------- data HandlerT = forall a. LogHandler a => HandlerT a data Logger = Logger { level :: Maybe Priority, handlers :: [HandlerT], name :: String} type LogTree = Map.Map String Logger {- | This is the base class for the various log handlers. They should all adhere to this class. -} --------------------------------------------------------------------------- -- Utilities --------------------------------------------------------------------------- -- | The name of the root logger, which is always defined and present -- on the system. rootLoggerName :: String rootLoggerName = "" --------------------------------------------------------------------------- -- Logger Tree Storage --------------------------------------------------------------------------- -- | The log tree. Initialize it with a default root logger -- and (FIXME) a logger for MissingH itself. {-# NOINLINE logTree #-} logTree :: MVar LogTree -- note: only kick up tree if handled locally logTree = unsafePerformIO $ do h <- streamHandler stderr DEBUG newMVar (Map.singleton rootLoggerName (Logger {level = Just WARNING, name = "", handlers = [HandlerT h]})) {- | Given a name, return all components of it, starting from the root. Example return value: >["", "MissingH", "System.Cmd.Utils", "System.Cmd.Utils.pOpen"] -} componentsOfName :: String -> [String] componentsOfName name' = let joinComp [] _ = [] joinComp (x:xs) [] = x : joinComp xs x joinComp (x:xs) accum = let newlevel = accum ++ "." ++ x in newlevel : joinComp xs newlevel in rootLoggerName : joinComp (split "." name') [] --------------------------------------------------------------------------- -- Logging With Location --------------------------------------------------------------------------- {- | Log a message using the given logger at a given priority. -} logM :: String -- ^ Name of the logger to use -> Priority -- ^ Priority of this message -> String -- ^ The log text itself -> IO () logM logname pri msg = do l <- getLogger logname logL l pri msg --------------------------------------------------------------------------- -- Utility functions --------------------------------------------------------------------------- {- | Log a message at 'DEBUG' priority -} debugM :: String -- ^ Logger name -> String -- ^ Log message -> IO () debugM s = logM s DEBUG {- | Log a message at 'INFO' priority -} infoM :: String -- ^ Logger name -> String -- ^ Log message -> IO () infoM s = logM s INFO {- | Log a message at 'NOTICE' priority -} noticeM :: String -- ^ Logger name -> String -- ^ Log message -> IO () noticeM s = logM s NOTICE {- | Log a message at 'WARNING' priority -} warningM :: String -- ^ Logger name -> String -- ^ Log message -> IO () warningM s = logM s WARNING {- | Log a message at 'ERROR' priority -} errorM :: String -- ^ Logger name -> String -- ^ Log message -> IO () errorM s = logM s ERROR {- | Log a message at 'CRITICAL' priority -} criticalM :: String -- ^ Logger name -> String -- ^ Log message -> IO () criticalM s = logM s CRITICAL {- | Log a message at 'ALERT' priority -} alertM :: String -- ^ Logger name -> String -- ^ Log message -> IO () alertM s = logM s ALERT {- | Log a message at 'EMERGENCY' priority -} emergencyM :: String -- ^ Logger name -> String -- ^ Log message -> IO () emergencyM s = logM s EMERGENCY --------------------------------------------------------------------------- -- Public Logger Interaction Support --------------------------------------------------------------------------- -- | Returns the logger for the given name. If no logger with that name -- exists, creates new loggers and any necessary parent loggers, with -- no connected handlers. getLogger :: String -> IO Logger getLogger lname = modifyMVar logTree $ \lt -> case Map.lookup lname lt of Just x -> return (lt, x) -- A logger exists; return it and leave tree Nothing -> do -- Add logger(s). Then call myself to retrieve it. let newlt = createLoggers (componentsOfName lname) lt let result = fromJust $ Map.lookup lname newlt return (newlt, result) where createLoggers :: [String] -> LogTree -> LogTree createLoggers [] lt = lt -- No names to add; return tree unmodified createLoggers (x:xs) lt = -- Add logger to tree if Map.member x lt then createLoggers xs lt else createLoggers xs (Map.insert x (defaultLogger {name=x}) lt) defaultLogger = Logger Nothing [] undefined -- | Returns the root logger. getRootLogger :: IO Logger getRootLogger = getLogger rootLoggerName -- | Log a message, assuming the current logger's level permits it. logL :: Logger -> Priority -> String -> IO () logL l pri msg = handle l (pri, msg) -- | Handle a log request. handle :: Logger -> LogRecord -> IO () handle l (pri, msg) = let parentLoggers :: String -> IO [Logger] parentLoggers [] = return [] parentLoggers name' = let pname = (head . drop 1 . reverse . componentsOfName) name' in do parent <- getLogger pname next <- parentLoggers pname return (parent : next) parentHandlers :: String -> IO [HandlerT] parentHandlers name' = parentLoggers name' >>= (return . concatMap handlers) -- Get the priority we should use. Find the first logger in the tree, -- starting here, with a set priority. If even root doesn't have one, -- assume DEBUG. getLoggerPriority :: String -> IO Priority getLoggerPriority name' = do pl <- parentLoggers name' case catMaybes . map level $ (l : pl) of [] -> return DEBUG (x:_) -> return x in do lp <- getLoggerPriority (name l) if pri >= lp then do ph <- parentHandlers (name l) sequence_ (handlerActions (ph ++ (handlers l)) (pri, msg) (name l)) else return () -- | Call a handler given a HandlerT. callHandler :: LogRecord -> String -> HandlerT -> IO () callHandler lr loggername ht = case ht of HandlerT x -> System.Log.Handler.handle x lr loggername -- | Generate IO actions for the handlers. handlerActions :: [HandlerT] -> LogRecord -> String -> [IO ()] handlerActions h lr loggername = map (callHandler lr loggername ) h -- | Add handler to 'Logger'. Returns a new 'Logger'. addHandler :: LogHandler a => a -> Logger -> Logger addHandler h l= l{handlers = (HandlerT h) : (handlers l)} -- | Remove a handler from the 'Logger'. Handlers are removed in the reverse -- order they were added, so the following property holds for any 'LogHandler' -- @h@: -- -- > removeHandler . addHandler h = id -- -- If no handlers are associated with the 'Logger', it is returned unchanged. -- -- The root logger's default handler that writes every message to stderr can -- be removed by using this function before any handlers have been added -- to the root logger: -- -- > updateGlobalLogger rootLoggerName removeHandler removeHandler :: Logger -> Logger removeHandler l = case hs of [] -> l _ -> l{handlers = tail hs} where hs = handlers l -- | Set the 'Logger'\'s list of handlers to the list supplied. -- All existing handlers are removed first. setHandlers :: LogHandler a => [a] -> Logger -> Logger setHandlers hl l = l{handlers = map (\h -> HandlerT h) hl} -- | Returns the "level" of the logger. Items beneath this -- level will be ignored. getLevel :: Logger -> Maybe Priority getLevel l = level l -- | Sets the "level" of the 'Logger'. Returns a new -- 'Logger' object with the new level. setLevel :: Priority -> Logger -> Logger setLevel p l = l{level = Just p} -- | Clears the "level" of the 'Logger'. It will now inherit the level of -- | its parent. clearLevel :: Logger -> Logger clearLevel l = l {level = Nothing} -- | Updates the global record for the given logger to take into -- account any changes you may have made. saveGlobalLogger :: Logger -> IO () saveGlobalLogger l = modifyMVar_ logTree (\lt -> return $ Map.insert (name l) l lt) {- | Helps you make changes on the given logger. Takes a function that makes changes and writes those changes back to the global database. Here's an example from above (\"s\" is a 'LogHandler'): > updateGlobalLogger "MyApp.BuggyComponent" > (setLevel DEBUG . setHandlers [s]) -} updateGlobalLogger :: String -- ^ Logger name -> (Logger -> Logger) -- ^ Function to call -> IO () updateGlobalLogger ln func = do l <- getLogger ln saveGlobalLogger (func l) -- | Allow graceful shutdown. Release all opened files/handlers/etc. removeAllHandlers :: IO () removeAllHandlers = modifyMVar_ logTree $ \lt -> do let allHandlers = mapFoldr (\l r -> concat [r, handlers l]) [] lt mapM_ (\(HandlerT h) -> close h) allHandlers return $ Map.map (\l -> l {handlers = []}) lt mapFoldr :: (a -> b -> b) -> b -> Map.Map k a -> b #if MIN_VERSION_containers(0,4,2) mapFoldr = Map.foldr #else mapFoldr f z = foldr f z . Map.elems #endif {- | Traps exceptions that may occur, logging them, then passing them on. Takes a logger name, priority, leading description text (you can set it to @\"\"@ if you don't want any), and action to run. -} traplogging :: String -- Logger name -> Priority -- Logging priority -> String -- Descriptive text to prepend to logged messages -> IO a -- Action to run -> IO a -- Return value traplogging logger priority' desc action = let realdesc = case desc of "" -> "" x -> x ++ ": " handler :: Control.Exception.SomeException -> IO a handler e = do logM logger priority' (realdesc ++ (show e)) Control.Exception.throw e -- Re-raise it in Control.Exception.catch action handler {- This function pulled in from MissingH to avoid a dep on it -} split :: Eq a => [a] -> [a] -> [[a]] split _ [] = [] split delim str = let (firstline, remainder) = breakList (isPrefixOf delim) str in firstline : case remainder of [] -> [] x -> if x == delim then [] : [] else split delim (drop (length delim) x) -- This function also pulled from MissingH breakList :: ([a] -> Bool) -> [a] -> ([a], [a]) breakList func = spanList (not . func) -- This function also pulled from MissingH spanList :: ([a] -> Bool) -> [a] -> ([a], [a]) spanList _ [] = ([],[]) spanList func list@(x:xs) = if func list then (x:ys,zs) else ([],list) where (ys,zs) = spanList func xs hslogger-1.3.1.0/src/0000755000000000000000000000000007346545000012444 5ustar0000000000000000hslogger-1.3.1.0/src/UTF8.hs0000644000000000000000000000314707346545000013533 0ustar0000000000000000-- Internal module to support UTF8 module UTF8 (toUTF8BS) where import Data.Char (ord) import Data.Bits import Data.Word (Word8) import qualified Data.ByteString as BS toUTF8BS :: String -> BS.ByteString toUTF8BS = BS.pack . encodeStringUtf8 -- | Encode 'String' to a list of UTF8-encoded octets -- -- Code-points in the @U+D800@-@U+DFFF@ range will be encoded -- as the replacement character (i.e. @U+FFFD@). -- -- The code is extracted from Cabal library, written originally HVR encodeStringUtf8 :: String -> [Word8] encodeStringUtf8 [] = [] encodeStringUtf8 (c:cs) | c <= '\x07F' = w8 : encodeStringUtf8 cs | c <= '\x7FF' = (0xC0 .|. w8ShiftR 6 ) : (0x80 .|. (w8 .&. 0x3F)) : encodeStringUtf8 cs | c <= '\xD7FF'= (0xE0 .|. w8ShiftR 12 ) : (0x80 .|. (w8ShiftR 6 .&. 0x3F)) : (0x80 .|. (w8 .&. 0x3F)) : encodeStringUtf8 cs | c <= '\xDFFF'= 0xEF : 0xBF : 0xBD -- U+FFFD : encodeStringUtf8 cs | c <= '\xFFFF'= (0xE0 .|. w8ShiftR 12 ) : (0x80 .|. (w8ShiftR 6 .&. 0x3F)) : (0x80 .|. (w8 .&. 0x3F)) : encodeStringUtf8 cs | otherwise = (0xf0 .|. w8ShiftR 18 ) : (0x80 .|. (w8ShiftR 12 .&. 0x3F)) : (0x80 .|. (w8ShiftR 6 .&. 0x3F)) : (0x80 .|. (w8 .&. 0x3F)) : encodeStringUtf8 cs where w8 = fromIntegral (ord c) :: Word8 w8ShiftR :: Int -> Word8 w8ShiftR = fromIntegral . shiftR (ord c) hslogger-1.3.1.0/testsrc/0000755000000000000000000000000007346545000013344 5ustar0000000000000000hslogger-1.3.1.0/testsrc/Tests.hs0000644000000000000000000000100607346545000014777 0ustar0000000000000000{- arch-tag: Tests main file Copyright (C) 2004 John Goerzen License: BSD3 -} module Tests(tests) where import Test.HUnit import System.Log tests = TestList [TestLabel "priority levels" priorityLevels] priorityLevels :: Test priorityLevels = TestList [ TestCase ( DEBUG <= DEBUG @=? True), TestCase ( DEBUG <= INFO @=? True), TestCase ( INFO <= WARNING @=? True), TestCase ( WARNING <= ERROR @=? True), TestCase ( INFO <= ERROR @=? True), TestCase ( ERROR > INFO @=? True) ] hslogger-1.3.1.0/testsrc/Tests.hs0000755000000000000000000000100607346545000015002 0ustar0000000000000000{- arch-tag: Tests main file Copyright (C) 2004 John Goerzen License: BSD3 -} module Tests(tests) where import Test.HUnit import System.Log tests = TestList [TestLabel "priority levels" priorityLevels] priorityLevels :: Test priorityLevels = TestList [ TestCase ( DEBUG <= DEBUG @=? True), TestCase ( DEBUG <= INFO @=? True), TestCase ( INFO <= WARNING @=? True), TestCase ( WARNING <= ERROR @=? True), TestCase ( INFO <= ERROR @=? True), TestCase ( ERROR > INFO @=? True) ] hslogger-1.3.1.0/testsrc/runtests.hs0000644000000000000000000000026407346545000015571 0ustar0000000000000000{- arch-tag: Test runner Copyright (C) 2004-2011 John Goerzen License: BSD3 -} module Main where import Test.HUnit import Tests main = runTestTT tests hslogger-1.3.1.0/testsrc/runtests.hs0000755000000000000000000000026407346545000015574 0ustar0000000000000000{- arch-tag: Test runner Copyright (C) 2004-2011 John Goerzen License: BSD3 -} module Main where import Test.HUnit import Tests main = runTestTT tests