In petroleum and natural gas exploration, lithology identification-analyzing rock types beneath the Earth's surface-is crucial for assessing hydrocarbon reservoirs and optimizing drilling strategies. Traditionally, this process relies on logging data such as gamma rays and resistivity, which often require manual interpretation, making it labor-intensive and prone to errors. To address these challenges, we propose a novel machine learning framework-contrastive learning-transformer-leveraging self-attention mechanisms to enhance the accuracy of lithology identification. Our method first extracts unlabeled samples from logging data while obtaining labeled core sample data. Through self-supervised contrastive learning and a transformer backbone network, we optimize performance using techniques like batch normalization. After pretraining, the model is fine-tuned with a limited number of labeled samples to improve accuracy and significantly reduce reliance on large labeled datasets, thereby lowering the costs associated with drilling core annotations. Additionally, our research incorporates shapley additive explanations (SHAP) technology to enhance the transparency of the model's decision-making process, facilitating the analysis of the contribution of each feature to lithology predictions. The model also learns time-reversal invariance by reversing sequential data, ensuring reliable identification even with variations in data sequences. Experimental results demonstrate that our transformer model, combined with semi-supervised contrastive learning, significantly outperforms traditional methods, achieving more precise lithology identification, especially in complex geological environments.