Custom encapsulation Logger demo small test, loggerdemo

Source: Internet
Author: User

Custom encapsulation Logger demo small test, loggerdemo

1. log4j. properties file:

Log4j. rootLogger = DEBUG, MINA, file

### Console ###
Log4j. appender. stdout = org. apache. log4j. leleappender
Log4j. appender. stdout. Target = System. out
Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout
Log4j. appender. stdout. layout. ConversionPattern = % d {ABSOLUTE} % 5 p % c {1}: % L-% m % n

Log4j. appender. MINA = org. apache. log4j. leleappender
Log4j. appender. MINA. layout = org. apache. log4j. PatternLayout
Log4j. appender. MINA. layout. conversionPattern = % d {yyyy-MM-dd HH \: mm \: ss, SSS} %-5 p % c {1} % x-% m % n

Log4j. appender. file = org. apache. log4j. RollingFileAppender
Log4j. appender. file. File = D:/logfile/minademos. log
Log4j. appender. file. MaxFileSize = 5120KB
Log4j. appender. file. MaxBackupIndex = 10
Log4j. appender. file. layout = org. apache. log4j. PatternLayout
Log4j. appender. file. layout. ConversionPattern = [luozhonghua-error] [% d] % p | % m | [% t] % C. % M (% L) % n


2 Log4jConfig:

Import org. apache. log4j. PropertyConfigurator;
Public class Log4jConfig {
Private static boolean isReload = true;


/**
* Load the log4j configuration file
*/
Public static void load (){
String path = Log4jConfig. class. getClass (). getResource ("/"). getPath ()
+ "Config/log4j. properties ";


System. out. println ("log4j configfile path =" + path );
PropertyConfigurator. configureAndWatch (path, 1000); // checks whether the file is modified at a specified interval and automatically reads the configuration again.
}


Public static void main (String [] args ){
Load ();
}


Private static void reload (){
If (isReload ){
Load ();
}
IsReload = false;
}


Public void setReload (boolean flag ){
IsReload = flag;
}
}


3. Logger File



Import org. apache. commons. logging. Log;
Import org. apache. commons. logging. LogFactory;


Public class Logger {

Private Log log = null;
Static {
Log4jConfig. load (); // load the log4j configuration file
}


Private Logger (){
Log = LogFactory. getLog (this. getClass ());
}


Private Logger (Class c ){
Log = LogFactory. getLog (c );
}


Private Logger (String className ){
Log = LogFactory. getLog (className );
}


Public static Logger getLogger (){
Return new Logger ();
}


Public static Logger getLogger (Class c ){


Return new Logger (c );
}


Public static Logger getLogger (String className ){
Return new Logger (className );
}


Public void trace (String info ){
If (log. isTraceEnabled ())
Log. trace (info );
}


Public void debug (String info ){
If (log. isDebugEnabled ())
Log. debug (info );
}


Public void info (String info ){
If (log. isInfoEnabled ())
Log.info (info );
}


Public void warn (String info ){
If (log. isWarnEnabled ())
Log. warn (info );
}


Public void error (String info ){
If (log. isErrorEnabled ())
Log. error (info );
}


Public void error (Object info, Throwable t ){
If (log. isErrorEnabled ())
Log. error (info + "," + t );
}

Public void fatal (String info ){
If (log. isFatalEnabled ())
Log. fatal (info );
}


Public boolean isTraceEnabled (){
Return log. isTraceEnabled ();
}


Public boolean isDebugEnabled (){
Return log. isDebugEnabled ();
}


Public boolean isInfoEnabled (){
Return log. isInfoEnabled ();
}


Public boolean isWarnEnabled (){
Return log. isWarnEnabled ();
}


Public boolean isErrorEnabled (){
Return log. isErrorEnabled ();
}


Public boolean isFatalEnabled (){
Return log. isFatalEnabled ();
}
}


4: Test

Public class test {


Static Logger log = Logger. getLogger (test. class );


Public static void main (String [] args ){
For (int I = 0; I <2; I ++ ){
Log.info ("---------- info ");
Log. debug ("---------- debug ");
Log. error ("---------- error ");


System. out. println ("***********************");
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}


The test is successful, but it seems to be available, but the log4j source code looks like a powerful Application Scenario



 


How can C call a C ++-encapsulated DLL? DEMO

[1] Static call
Copy TestDLL. lib and TestDLL. dll to your test project.
Your dll declaration method _ declspec (dllexport) is to call the dll statically, so you need to add the following code:
//*/

# Pragma comment (lib, "TestDLL. lib")/* (1) contains the library file. If you do not write this sentence, you need to include this library through program configuration */
_ Declspec (dllimport) int clFun (int a, int B);/* (2) declare that the function is called from the dll. Do not write this sentence, you need to include the header file */

Void main ()
{
Int a = 1, B = 2, c = 0;
C = clFun (a, B );
}

/*
[2] dynamic call

If you want to use the LoadLibrary method, it is a dynamic call. In this case, you do not need to add your declaration _ declspec (dllexport), but only need to be in the TestDLL project directory. add the name of the function to be called to the def file.

; TestDLL. def: Declares the module parameters of the DLL.
LIBRARY "TestDLL"
EXPORTS
In this example, it can be explicitly exported.
ClFun
]
Next, copy TestDLL. dll to your test project and add the following code:
//*/

Void main ()
{
Int a = 1, B = 2, c = 0;

// Define a function pointer.
Typedef int (* Any_name) (int param1, int param2 );

// Define a function pointer variable
Any_name pfFuncInDll = NULL;

// Load the dll
HINSTANCE hinst = LoadLibrary (L "TestDLL. dll ");

If (hinst! = NULL)
{
// Find the clFun function of the dll
PfFuncInDll = (Any_name) GetProcAddress (hinst, "clFun ");

// Call the functions in the dll
If (pfFuncInDll! = NULL)
{
C = pfFuncInDll (a, B );
}
FreeLibrary (hinst );
}
}... Remaining full text>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.