Fungsi print()_ berguna untuk mencetak variabel atau kemajuan perhitungan ke konsol (walaupun tidak terlalu sering; pencetakan membutuhkan waktu dan memperlambat perhitungan). Namun, untuk pelaporan kesalahan yang kuat atau pesan penting lainnya, file log merupakan pilihan yang lebih baik. Jadi apa itu file log atau tindakan logging? . Demikian pula, sebuah kode dapat menuliskan (log) "pengalaman" -nya, tetapi dalam file, bukan buku. Untuk tujuan ini, Python memiliki pustaka logging standar. Untuk saat ini, cukup mengetahui bahwa pustaka logging dapat diimpor dalam skrip Python apa pun dengan import logging (informasi lebih lanjut tentang paket, modul, dan pustaka disediakan di bagian ini)
Blok kode di bawah mengimpor modul logging, dan menggunakan argumen kata kunci berikut untuk menyetel logging.basicConfig
filename="my-logfile.log"_ membuat modul logging menulis pesan ke file bernama "my-logfile.log" di direktori yang sama di mana skrip Python sedang berjalan
format="%(asctime)s - %(message)s menyetel format pencatatan ke YYYY-MM-DD HH:MM:SS.sss -Teks pesan (opsi format lainnya tercantum dalam )
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 0 menimpa pesan sebelumnya di file log (hapus argumen ini untuk menambahkan pesan alih-alih menimpa)
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 1 mendefinisikan tingkat keparahan pesan yang ditulis ke file log, di mana This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 2 cukup untuk diagnosis masalah dalam kode;
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 3 untuk menulis semua pesan konfirmasi acara yang bekerja seperti yang diharapkan
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 4 (default) untuk menunjukkan kapan kejadian tak terduga terjadi atau saat kejadian dapat menyebabkan kesalahan di masa mendatang (e. g. , karena ruang disk tidak mencukupi)
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 5 untuk melaporkan masalah serius yang membuat kode mogok atau tidak menghasilkan hasil yang diinginkan
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 6 adalah indikator masalah serius yang lebih luas, di mana program itu sendiri mungkin tidak dapat terus berjalan (mis. g. , tidak hanya kodenya tetapi juga Python mogok)
Sampai di sini, pesan hanya ditulis ke file log, tetapi kami tidak dapat melihat pesan apa pun di konsol. Untuk mengaktifkan logging simultan ke file log dan konsol (terminal Python), gunakan This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 7 yang menambahkan io stream handler
Untuk menulis pesan ke file log (dan terminal Python), gunakan
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. 8 untuk diagnosis kode,
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric. _9 untuk informasi kemajuan (seperti yang kita gunakan 2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. 0 sebelumnya),
2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. _1 untuk dokumentasi kejadian tak terduga (tanpa gangguan program),
2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. _2 untuk kesalahan yang menyebabkan kerusakan kode, dan
2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. _3 untuk kesalahan kritis yang dapat menyebabkan crash program (Python)
Peringatan, kesalahan, dan pesan penting harus diterapkan dalam pengecualian yang muncul (lihat di atas 2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. 4 - 2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. 5 pernyataan)
Di akhir skrip, logging harus dihentikan dengan 2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. 6 karena jika tidak, file log dikunci oleh Python dan Kernel Python harus dihentikan untuk melakukan modifikasi (mis. g. memindahkan kembali) ke file log
import logging logging.basicConfig(filename="my-logfile.log", format="%(asctime)s - %(message)s", filemode="w", level=logging.DEBUG) logging.getLogger().addHandler(logging.StreamHandler()) logging.debug("This message is logged to the file.") logging.info("Less severe information is also logged to the file.") logging.warning("Warning messages are logged, too.") a = "text" try: logging.info(str(a**2)) except TypeError: logging.error("The variable is not numeric.") # stop logging logging.shutdown()
This message is logged to the file. Less severe information is also logged to the file. Warning messages are logged, too. The variable is not numeric.
Dan beginilah tampilan 2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. _7
2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric.
Peristiwa juga dapat didokumentasikan dengan membuat instance objek logger dengan 2050-00-00 18:51:46,657 - This message is logged to the file. 2050-00-00 18:51:46,666 - Less severe information is also logged to the file. 2050-00-00 18:51:46,667 - Warning messages are logged, too. 2050-00-00 18:51:46,669 - The variable is not numeric. 8. Solusi yang menguntungkan ini direkomendasikan untuk pengkodean tingkat lanjut seperti menulis ) baru (baca lebih lanjut di ). Contoh skrip dengan logger yang lebih canggih disediakan dengan skrip Logger di repositori kursus