logging - 使用带有可选元素的额外dict格式化

时间:2015-10-23 10:52:26

标签: python logging

我想达到这样的结果:

func savePlaceToCloud (id: Int, latitude: Double, longitude: Double, title: String, additionalInfo: String) {
    let place = CKRecord(recordType: "Place")
    place.setValue(id, forKey: "id")
    place.setValue(latitude, forKey: "latitude")
    place.setValue(longitude, forKey: "longitude")
    place.setValue(title, forKey: "title")
    place.setValue(additionalInfo, forKey: "additionalInfo")
    print(place)

    publicDB.saveRecord(place, completionHandler: { (record, error) -> Void in
        print(record)
        NSLog("saved to iCloud")
    })
}
# code
logging.basicConfig(format='%(levelname)s: %(sublevel) %(message)s', 
                    level=logging.DEBUG)

logging.debug("abc", extra={'sublevel':2})
logging.debug("def", extra={'sublevel':1}) 
logging.debug("ghi")

问题是我没有在额外的dict中定义sublevel项目,我不知道如何将# output DEBUG: abc DEBUG: abc DEBUG:abc 的数值更改为sublevel

1 个答案:

答案 0 :(得分:1)

使用自定义public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener { private static String TAG = MainActivity.class.getSimpleName(); private Toolbar mToolbar; private FragmentDrawer drawerFragment; private String chng; private Intent i; public SearchView searchView; public SearchManager searchManager; // Alert Dialog Manager AlertDialogManager alert = new AlertDialogManager(); // Session Manager Class SessionManager session; private String id; private boolean b; private String rasa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); session = new SessionManager(getApplicationContext()); id = getIntent().getStringExtra("id"); System.out.println("MAIN ID : " + id); /* i=getIntent(); chng=i.getStringExtra("Changes");*/ // Toast.makeText(getApplicationContext(),chng,Toast.LENGTH_LONG).show(); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false); drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout),mToolbar); drawerFragment.setDrawerListener(this); displayView(0); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } 子类,该子类使用Formatter值来格式化具有适当空格数的输出字符串,或使用sublevel并使用格式中的extra={'prefix': ' ' * sublevel}带有普通"%(levelname)s: %(prefix)s %(message)s"的字符串。